IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
-Collapse +Expand Members-Only
Sign in to see member-only pages.
   ► KBTo/From GuidesVB ClassicOperators  Print This     

VB Classic 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.

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




Cross Reference Examples:

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
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
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");
}
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");
}
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
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');
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");
Java:   IsEmpty
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




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


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