IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
   ► KBTo/From GuidesReferenceOperators  Print This     

Empty String Check (Cross Ref > Operators)

Empty String Check

Languages Focus

An empty string is a zero length string, a string that is equal to null (""), or not assigned. In some languages, you can check if a string is empty by comparing it to an empty string (""). Some languages distinguish between nil and null ("") so checking if the length is 0 is easier.

Access VBA:   Len(s&vbNullString)

In Access VBA, you have to add an empty string to the value being compared in order to get consistent results. For example, add &"" to your string varilable or it's code equivalent &vbNullString. Then compare to an empty string or verify it's length to 0 with Len.

Syntax Example:

All these will work for variables unassigned, set to "", or set to Null:

If s&"" = "" Then
  MsgBox ("Quotes with &'' say null is empty")
End If
 
If Len(s&"") = 0 Then
  MsgBox ("Len with &'' says null is empty")
End If
 
If Len(s&vbNullString) = 0 Then
  MsgBox ("Using vbNullString also works!")
End If

More Info

ASP Classic:   Len(s&vbNullString)

In ASP Classic, you have to add an empty string to the value being compared in order to get consistent results. For example, add &"" to your string varilable or it's code equivalent &vbNullString. Then compare to an empty string or verify it's length to 0 with Len.

Syntax Example:

All these will work for variables unassigned, set to "", or set to Null:

If s&"" = "" Then
  Response.Write("<br>Quotes with &'' say null is empty")
End If
 
If Len(s&"") = 0 Then
  Response.Write("<br>Len with &'' says null is empty")
End If
 
If Len(s&vbNullString) = 0 Then
  Response.Write("<br>Using vbNullString also works!")
End If

Complete Example

In the following code, notice techniques notice techniques 2a and 2c fail with Null values.

<%@LANGUAGE='VbScript'%>
<%Option Explicit%>
<html>
<body>
<h1>Empty String Check</h1>
<%
Dim s

''''''''''''''''''''''''''''
''' 1. Unassigned value. '''
''''''''''''''''''''''''''''
''Technique A
'''''''''''''
If s = "" Then
  Response.Write("<br>1a. Quotes say unassigned empty")
End If
 
''Technique B
'''''''''''''
If s&"" = "" Then
  Response.Write("<br>1b. Quotes say unassigned empty")
End If
 
''Technique C
'''''''''''''
If Len(s) = 0 Then
  Response.Write("<br>1c. Len says unassigned is empty")
End If
 
''Technique D
'''''''''''''
If Len(s&"") = 0 Then
  Response.Write("<br>1d. Len with &'' says unassigned is empty")
End If

''''''''''''''''''''''
''' 2. Null value. '''
''''''''''''''''''''''
s = Null
 
''Technique A
'''''''''''''
If s = "" Then
  Response.Write("<br>2a. Quotes say null is empty") ''Fails!!!
End If
 
''Technique B
'''''''''''''
If s&"" = "" Then
  Response.Write("<br>2b. Quotes with &'' say null is empty")
End If
 
''Technique C
'''''''''''''
If Len(s) = 0 Then
  Response.Write("<br>2c. Len says null is empty") ''Fails!!!
End If
 
''Technique D
'''''''''''''
If Len(s&"") = 0 Then
  Response.Write("<br>2d. Len with &'' says null is empty")
End If

''''''''''''''''''''
''' 3. "" value. '''
''''''''''''''''''''
s = ""
 
''Technique A
'''''''''''''
If s = "" Then
  Response.Write("<br>3a. Quotes say '' is empty") ''Fails!!!
End If
 
''Technique B
'''''''''''''
If s&"" = "" Then
  Response.Write("<br>3b. Quotes with &'' say '' is empty")
End If
 
''Technique C
'''''''''''''
If Len(s) = 0 Then
  Response.Write("<br>3c. Len says '' is empty") ''Fails!!!
End If
 
''Technique D
'''''''''''''
If Len(s&"") = 0 Then
  Response.Write("<br>3d. Len with &'' says '' is empty")
End If
 
''vbNullString is the code version of ""
''''''''''''''''''''''''''''''''''''''''
If Len(s&vbNullString) = 0 Then
  Response.Write("<br>Using vbNullString also works!")
End If
%>
</body>
</html>


Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

Advanced

1 Advanced Level Question

Question #1: Multiple Choice

Will the following code print "Hello, World!" or will it cause an error?

<%@LANGUAGE='VbScript'%>
<%Option Explicit%>
<html>
<body>
<%
Dim s
 
s = Null
  
If Len(s) = 0 Then
  Response.Write("Hello, World!")
End If
%>
</body>
</html>
Answer:
1. 

It will cause an error.

2. 

It will print "Hello, World!".

More Info

Code:  ASP Classic Empty String Check (Len(s&vbNullString))

C#:   String.IsNullOrEmpty

The .Net framework offers a static method in the string class: String.IsNullOrEmpty.

Syntax Example:
String s;
 
s = "";
//s = null; //Uncomment to test 2nd case.
 
if (String.IsNullOrEmpty(s))
{
  MessageBox.Show("empty string");
}

More Info

C++/CLI:   String.IsNullOrEmpty

The .Net framework offers a static method in the string class: String.IsNullOrEmpty.

Syntax Example:
String^ s;
  
//s = ""; //Uncomment to test 2nd case.
  
if (String::IsNullOrEmpty(s))
{
  MessageBox::Show("empty string");
}

More Info

Corel Paradox:   isBlank() or not isAssigned()

In ObjectPAL, an empty variable can be unassigned (essentially null) or blank (equivalent to ""). You have to use both isBlank and isAssigned to check for an empty string.

Syntax Example:
var
 s String
endVar
  
;s = ""  ;Uncomment to test 2nd case.
 
if isBlank(s) or not isAssigned(s) Then
 msgInfo("", "empty string")
endIf

Testing Out isBlank and isAssigned Code

Add the following code to a pushButton event of a button to test out isBlank and isAssigned.

var
 s String
endVar
 
;Neither s="" not size(s)=0 work with unassigned!!!
;if s = "" Then
;if size(s) = 0 then

;Try not assigning at all and then
;uncomment the following line of code.
s = ""
 
if isBlank(s) then
 msgInfo("", "blank")
endIf
 
;unAssign(s) ;Uncomment to test following.
 
if not isAssigned(s) then
 msgInfo("", "not assigned")
endIf

More Info

Code:  ObjectPAL Empty String Check (isBlank() or not isAssigned())

Delphi:   length(s) = 0

Length() or SizeOf() will correctly identify an unassigned string variable or an empty string.

Syntax Example:
if length(s) = 0 then
  ShowMessage('empty string');

More Info

Delphi Prism:   length

In Prism, a string can be nil (unassigned), assigned an empty string (""), or assigned a value.  Therefore, to check if a string is empty, you have to check against both nil and (""). Alternatively, you can check the length of the string or use String.IsNullOrEmpty.

Syntax Example:
var s: String; 
 
if (s = nil) or (s = '') then
  MessageBox.Show("empty string");

or use length:

if length(s) = 0 then
  MessageBox.Show("empty string");

Using IsNullOrEmpty

The .NET framework provides a IsNullOrEmpty static method as part of the String class. Remember, the syntax to use a static method is always ClassName.MethodName and you do not have to create the class ahead of time because it is static.

var s: String;
 
//Unassigned check.
If String.IsNullOrEmpty(s) then
MessageBox.Show("empty or null string");
 
//Assigned a non-zero value.
s := "health care reform";
 
If Not String.IsNullOrEmpty(s) then
MessageBox.Show("string contains a non zero value");
 
//Empty string.
s := "";
 
If String.IsNullOrEmpty(s) then
MessageBox.Show("empty or null string");

 

More Info

Code:  Delphi Prism Empty String Check (length)

Java:   IsEmpty

VB Classic:   Len(s&vbNullString)

In VB Classic, you have to add an empty string to the value being compared in order to get consistent results. For example, add &"" to your string varilable or it's code equivalent &vbNullString. Then compare to an empty string or verify it's length to 0 with Len.

Syntax Example:

All these will work for variables unassigned, set to "", or set to Null:

If s&"" = "" Then
MsgBox ("Quotes with &'' say null is empty")
End If
 
If Len(s&"") = 0 Then
MsgBox ("Len with &'' says null is empty")
End If
 
If Len(s&vbNullString) = 0 Then
MsgBox ("Using vbNullString also works!")
End If

More Info

VB.Net:   String.IsNullOrEmpty

In .Net, a string can be null or empty. The .Net framework offers a static method in the string class: String.IsNullOrEmpty to check if a string is null or empty.

Syntax Example:
Dim s As String 
  
's = "" 'Uncomment to test 2nd case.
  
If String.IsNullOrEmpty(s) Then
  MessageBox.Show("hello")
End If


Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

Beginner

1 Beginner Level Question

Question #1: True or False?

In .Net, a string can be null or empty. The .Net framework offers a static method in the string class: String.IsNullOrEmpty to check if a string is null or empty.

Answer:
  • True
  • False
  • More Info

     




    Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
    www.prestwood.com


    ©1995-2024 Prestwood IT Solutions.   [Security & Privacy]