Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String is not null, empty, or empty string

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

like image 982
TruthOf42 Avatar asked Sep 29 '14 19:09

TruthOf42


People also ask

Is string not NULL or empty?

You can use the IsNullOrWhiteSpace method to test whether a string is null , its value is String. Empty, or it consists only of white-space characters.

How do you check if a string is empty or not?

The isEmpty() method checks whether a string is empty or not. This method returns true if the string is empty (length() is 0), and false if not.

How do you check if a string is not NULL or empty in Java?

isEmpty(String) , which checks if a string is an empty string or null. It also provides the StringUtils. isBlank(String) method, which also checks for whitespace. That's all about determining whether a String is empty or null in Java.

Is string NULL or empty C#?

In C#, IsNullOrEmpty() is a string method. It is used to check whether the specified string is null or an Empty string. A string will be null if it has not been assigned a value. A string will be empty if it is assigned “” or String.


2 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
like image 172
Ekkehard.Horner Avatar answered Sep 29 '22 00:09

Ekkehard.Horner


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
like image 28
Rocky Avatar answered Sep 28 '22 22:09

Rocky