Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


How to check an empty string in C#

Posted on 2012-08-29
Medium Priority
Last Modified: 2012-08-30
Hi experts
I am trying to check for an empty string but I wonder why the following doesn't work.
  string s = "  "; // two empty spaces.
  bool flag = string.IsNullOrEmpty(s); // gives false.
Question by:dominicwong
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 800 total points
ID: 38349020
because "  " is not a empty string.

you would need to check with s.Trim(), but then again, if s is null, that would raise a "objectnotset" exception.
LVL 20

Assisted Solution

by:Rikin Shah
Rikin Shah earned 100 total points
ID: 38349034
LVL 45

Assisted Solution

AndyAinscow earned 200 total points
ID: 38349047
If you require to check often that a string of blank spaces is 'empty' (as angelIII says it is not actually empty) then you might want to create your own string class based on the System.String or a function to do the job for you:

bool IsNullOrEMPTY(String s)
  //test for null and return if null

  //trim blank spaces off the ends

  //test if empty and return
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 12

Accepted Solution

rajapandian_81 earned 800 total points
ID: 38349048

Framework 4.0 has string method called IsNullOrWhiteSpace. It will return true if the specified string is null, empty, or consists only of white-space characters.
string s = "  ";
bool flag = string.IsNullOrWhiteSpace(s);

Open in new window

For older framework you should use
string s = "  ";
bool flag = String.IsNullOrEmpty(s) || s.Trim().Length == 0;

Open in new window

LVL 45

Assisted Solution

AndyAinscow earned 200 total points
ID: 38349072
IsNullOrWhitespace - a blank is NOT the only whitespace character.

Assisted Solution

aplusexpert earned 100 total points
ID: 38349496

Author Closing Comment

ID: 38352606
I forgot to mention that mine is .NET 3.5
Thanks to the prompts responses and help from everyone. I appreciate that.

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question