What is the quickest and easiest way (in Classic ASP) to check if a string has some string (that has a length greater than 0) i.e. NOT "Null", "Nothing", "Empty", or '' empty string
7 Answers
To make sure that the Variant you deal with is of sub-type "string", you need the VarType or TypeName function. To rule out zero length strings, you need Len(). To guard against strings of space, you could throw in a Trim().
Code to illustrate/experiment with:
Option Explicit
Function qq(s) : qq = """" & s & """" : End Function
Function toLiteral(x)
Select Case VarType(x)
Case vbEmpty
toLiteral = "<Empty>"
Case vbNull
toLiteral = "<Null>"
Case vbObject
toLiteral = "<" & TypeName(x) & " object>"
Case vbString
toLiteral = qq(x)
Case Else
toLiteral = CStr(x)
End Select
End Function
Function isGoodStr(x)
isGoodStr = False
If vbString = VarType(x) Then
If 0 < Len(x) Then
isGoodStr = True
End If
End If
End Function
Dim x
For Each x In Array("ok", "", " ", 1, 1.1, True, Null, Empty, New RegExp)
WScript.Echo toLiteral(x), CStr(isGoodStr(x))
Next
output:
cscript 26107006.vbs "ok" True "" False " " True 1 False 1.1 False True False <Null> False <Empty> False <IRegExp2 object> False
Comments
Here's a one-liner that dodges all the trouble with Null by concatenating the value with an empty string. It works for Null, Empty, "", and, of course, strings with actual length! The only one it doesn't (nor shouldn't) work for is Nothing, because that's for object variables, of which a string is not.
isNullOrEmpty = (Len("" & myString) = 0)
Comments
You could try having something like this:
Function nz(valToCheck, valIfNull)
If IsNull(valToCheck) then
nz = valIfNull
Else
nz = valToCheck
End if
End function
and then you would use it like this:
if nz(var,"") <> "" then
'--string has something in it
else
'--string is null or empty
end is
1 Comment
--string has something in it. That will help more users.You can use the VarType() function to check if it is a string, then you can check if the string is not empty. This statement will only pass through a string that isn't empty.
If VarType(MyString) = 8 Then
If MyString <> "" Then
'String is Not Null And Not Empty, code goes here
End If
End If
8 Comments
VarType(MyString) > 1 is flawed.I use this function in all my projects :
'Returns True if [Empty] or [NULL] or [Empty String] or [Empty Object]
Function IsBlank(Value)
If IsEmpty(Value) or IsNull(Value) Then
IsBlank = True
ElseIf VarType(Value) = vbString Then
If Value = "" Then
IsBlank = True
Else
IsBlank = False
End If
ElseIf IsObject(Value) Then
If Value Is Nothing Then
IsBlank = True
Else
IsBlank = False
End If
Else
IsBlank = False
End If
End Function 'IsBlank
Len(s) > 0. If you need to verify your variable's type to ensure it's a string, look to the (more complicated) answers below.Len(s & "") > 0avoids having to check for null values, similar to what @Bond is suggesting but with an implicit cast on thesvariable.s <> ""is sufficient. It is also null proof sincenull <> ""is null which can be used insideifand the branch will not execute. It also works for types other than string... they are cast to string for comparison which is the safest cast and won't give you "type mismatch" errors.