String is not null, empty, or empty string

后端 未结 6 871
礼貌的吻别
礼貌的吻别 2021-01-04 07:18

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 \

6条回答
  •  半阙折子戏
    2021-01-04 07:52

    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 = ""
        Case vbNull
          toLiteral = ""
        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
     False
     False
     False

提交回复
热议问题