PrestwoodBoards.com
-Collapse +Expand

ASP Classic

Search ASP Classic Group:

Advanced
-Collapse +Expand ASP Classic Group Home
-Collapse +Expand Message Board
-Collapse +Expand Knowledge Base
-Collapse +Expand ASP Classic To/From
To/FromCODEGuides
-Collapse +Expand ASP Classic Study Test
PRESTWOODCERTIFIED
-Collapse +Expand ASP Classic Store
-Collapse +Expand Members Only
Prestwood Tip Jar
Tip Jar
Finding something useful?

Add to the
Tip Jar!

Prestwood eMagazine

Subscribe now!
Enter your email:


   PrestwoodBoardsKBWebsite ScriptingASP Classic   
Go To Random Article
  From the May 2010 Issue of Prestwood eMag
 
ASP Classic Empty String Check (Len(s&vbNullString))
By Mike Prestwood
9/17/2009, Last updated 7/21/2010
 
ASP Classic Code Snippet:
 A flashcard from our ASP Classic Flashcards Library
 A code snippet from our ASP Classic Code Snippets Page


Languages Focus: Empty String Check

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.

ASP Classic Empty String Check

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>

More Info

Code Contributed By Prestwood staff member Mike Prestwood:
mprestwood
Email Approved! E CA USA

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.



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!".


Comments

Oldest To Newest

Add Comment
Reader...
Anonymous Post
Comment 1 of 1
Wednesday, July 21, 2010

Thank you so much! Beautiful and profound explanation!

---
Atos
Would you like to comment? Reply? Ask a question? Say thanks?
+Add Comment

 KB Article #102039 Counter
1002
Since 9/17/2009

Sponsored Ad
Brought to you by Prestwood IT Solutions
We hope you are enjoying our knowledge base! We welcome your participation in our open online community. As the caretakers, we pay our staff to moderate, edit, and contribute free content here in an effort to promote our software development company. Keep us in mind if you or your company needs help. Whether you need a single developer or a team, consider Prestwood. We are an American company that keeps jobs in America (no off shoring and no need for H-1B developers). Our hope is that you will put some or all of your development tasks in our hands. We specialize in hourly help (1 hour minimum), custom websites from $2k to $40k, business database applications from $5k to $60k, and enterprise application development starting from $20k. Talented Developer?
If you are a talented developer and would like to work with us, start by filling out our Register for Work form.
 
704 People Online Now!!  
Online Now: Sign In to see who's online now!  Not a member? Join Prestwood now. It's free!