String is not null, empty, or empty string

TruthOf42 picture TruthOf42 · Sep 29, 2014 · Viewed 57.7k times · Source

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

Answer

Ekkehard.Horner picture Ekkehard.Horner · Sep 29, 2014

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