I.T. Discussion Community!
-Collapse +Expand
Prism
Search Prism Group:

Advanced
-Collapse +Expand Prism To/From
To/FromCODEGuides
-Collapse +Expand Prism Study Test
PRESTWOODCERTIFIED
-Collapse +Expand Prism Store
PRESTWOODSTORE

Prestwood eMagazine

April Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi Prism   Print This     
  From the November 2015 Issue of Prestwood eMag
 
Delphi Prism Empty String Check (length)
 
Posted 10 years ago on 9/16/2009
Prism Code Snippet:
 A flashcard from our Prism Flashcards Library
 A code snippet from our Prism Code Snippets Page

KB102037



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.

Delphi Prism Empty String Check

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


Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1196A1
Enter key:
Code Contributed By Mike Prestwood:

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.

Visit Profile

 KB Article #102037 Counter
14736
Since 9/16/2009
-
   Contact Us!
 
PrestwoodBoards.com was developed and is maintainted by me. Do you have a question or suggestion? Do you see a problem? Contact me now. My goal is to build an ad-free and spam-free source of I.T. information with many contributers (ok to promote your website/company in your bio). Yes, my company Prestwood IT Solutions is mentioned in my bio which shows with every post, but you can contribute and promote your pet project too!

2,854 People Online Now!!  
Sign In to see who's online now!  Not a member? Join now. It's free!
Show more stats...


©1995-2019 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech