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
-
Is there a String class in classic ASP? – Lajos Arpad Sep 29 '14 at 19:23
-
3@LajosArpad Classic ASP is not a language it is a technology that can handle server-side VBScript or Javascript code. Therefore, VBScript does not have a string class (like .net/C#), but does have functionality to manipulate strings and variables. – Control Freak Sep 29 '14 at 20:31
-
1If you have a string variable, as I understand your question, just test it with `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. – Bond Sep 29 '14 at 21:11
-
4If you're always expecting a string just use `Len(s & "") > 0` avoids having to check for null values, similar to what @Bond is suggesting but with an implicit cast on the `s` variable. – user692942 Sep 30 '14 at 08:59
-
Checking `s <> ""` is sufficient. It is also null proof since `null <> ""` is null which can be used inside `if` and 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. – Salman A Jul 11 '18 at 08:56
6 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

- 16,398
- 7
- 76
- 175

- 38,498
- 2
- 45
- 96
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)

- 34,009
- 10
- 83
- 174
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

- 494
- 6
- 18
-
It would be great if you can show a complete script rather than saying `--string has something in it`. That will help more users. – jazzurro Oct 04 '14 at 00:38
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

- 12,965
- 30
- 94
- 145
-
2Please test your code before you post. VarType of "" and Trim(" ") is 8 (string). So your idea to check `VarType(MyString) > 1` is flawed. – Ekkehard.Horner Sep 29 '14 at 20:38
-
@Ekkehard.Horner You're right thanks, i didn't notice that. Anyways, I fixed it. – Control Freak Sep 29 '14 at 20:44
-
Now numbers, booleans, and everything that Len() stringyfies will pass as 'good' strings. – Ekkehard.Horner Sep 29 '14 at 21:01
-
-
3You shouldn't use And, but a nested If because VBScript evaluates both sub-expressions. For non-strings Len(MyString) is unnecessary/inefficient, for objects even fatal. – Ekkehard.Horner Sep 29 '14 at 21:15
-
Thanks for going the extra mile (and the idea of comparing against ""). – Ekkehard.Horner Sep 29 '14 at 21:26
-
Who's answer is this now @Ekkehard.Horner?, I can barely recognise it. ;) – user692942 Sep 30 '14 at 09:01
-
1@Lankymart What can I say? The struggles of learning new technology risks forgetting the old. – Control Freak Oct 01 '14 at 13:09
This worked for me:
if mystring = "" then wscript.echo "Empty string"
else wscript.echo "String is not empty"

- 19
- 2
<%
Dim x,y
x = "abcdefg"
'counting length of string
y = Len(x)
Response.Write (y)
'checking string is empty or not
If Len(x) = 0 then
Response.Write ("<p>String is empty</p>")
Else
Response.Write ("<p>String is not empty</p>")
End If
%>
Hope this is helpful.

- 21