Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

String.IsNullOrEmpty

, 27 Dec 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
String.IsNullOrEmpty

Maybe some of you are already using this. But for others, it could be a great utility method.

We use a static method IsNullOrEmpty of string in most of our daily task/projects a lot. It works fine in most of the cases, but it could work bizarre in some cases if not handled properly.

Say, you get a entry from UI, and having a check whether user has entered something or not. It would work fine as long as user enters the data or not.

But what happen if user enters just spaces. This method would return false, ironically this method is behaving as coded. But do we need this?

Obviously not, spaces could lead to wrong entry in our database even can corrupt the data and leads to uncommon results/Y SODS/malfunctioning.

Although being a good developer, one always trims the input before having the check. But it also tends a lots of extra LOCs which we can save and can make our system less error prone.

Most of us are aware of the beauty of the extension methods that were introduced in C# 3.0. So we can have an extension method over a string, which actually does both; first trimming and then checking for IsNullOrEmpty.

So we can have the extension method as:

public static bool IsNullorEmpty(this String val)
 {
      if (val != null)
      {
          if (string.IsNullOrEmpty(val.Trim()))
              return true;
          else
              return false;
      }
      return true;
 }

One more smarter code would be

public static bool IsNullorEmpty(this String val)
 {
      if (val != null)
      {
          return string.IsNullOrEmpty(val.Trim());
      }
      return true;
 }

Let's see both the existing and our new method running.

My code is:

static void Main(string[] args)
 {
     string name = "   ";
     Console.WriteLine(String.IsNullOrEmpty(name));
     Console.WriteLine(name.IsNullorEmpty());
     Console.ReadKey();
 }

The output is:

Output

But if you are still using .NET 2.0, you can have a normal static method in your utility call, which does the job for us.

public static bool CheckNullorEmpty(string val)
 {
     if (val != null)
     {
         if (string.IsNullOrEmpty(val.Trim()))
             return true;
         else
             return false;
     }
     return true;
 }

Note: I must say, the limitation of the existing IsNullorEmpty has been resolved in .NET 4.0. It means you don’t need to do all this. There is a new method String.IsNullOrWhiteSpace will do both for you. But unlikely, most of us are still working on .NET2.0/3.5.

Hope you all must have liked it.


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Brij
Software Developer (Senior)
India India
Brij is a 3-times Microsoft MVP in ASP.NET/IIS Category and a passionate .NET developer. More than 6 years of experience in IT field, currently serving a MNC as a Tech Lead/Architect.
 
He is a very passionate .NET developer and have expertise over Web technologies like ASP.NET 2.0/3.5/4.0, jQuery, JSON, Javascript, IIS and related technologies. He is also a Exchange Server (EWS) Specialist. He has great experience in design patterns and N-Tier Architecture.
 
He is also certified as Microsoft Certified Technologies Specialist-ASP.NET and Microsoft Certified Technologies Specialist-WCF in .NET 4.0. He has also received several awards at various forums and his various articles got listed as "Article of the day" at ASP.NET Microsoft Official Website www.asp.net.
 
He has done MCA from NIT Durgapur and completed his graduation from Lucknow University.
 
Learning new technologies and sharing knowledge excites him most. Blogging, solving problems at various forums, helping people, keeps him busy entire day.


Visit his Blog: Brij's arena of .NET
 
Area of Expertise :
C#, ASP.NET 2.0,3.5,4.0, AJAX, JQuery, JSON, XML, XSLT, ADO.Net, WCF, Active Directory, Exchange Server 2007 (EWS), Java script, Web Services ,Win services, DotnetNuke, WSS 3.0,Sharepoint Designer, SQL Server 2000/2005/2008
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 5 PinmemberSudeep35026-Dec-10 19:25 
GeneralRe: My vote of 5 PinmentorBrij27-Dec-10 6:52 
GeneralA bit faster one PinmemberSalarSoft25-Dec-10 8:21 
GeneralRe: A bit faster one PinmentorBrij26-Dec-10 2:21 
GeneralRe: A bit faster one PinmentorBrij27-Dec-10 7:02 
GeneralRe: A bit faster one Pinmemberbrad.ford@cudl.com27-Dec-10 7:07 
GeneralRe: A bit faster one PinmentorBrij27-Dec-10 7:26 
GeneralRe: A bit faster one Pinmemberbrad.ford@cudl.com27-Dec-10 8:59 
GeneralRe: A bit faster one PinmemberRichard Deeming4-Jan-11 9:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 27 Dec 2010
Article Copyright 2010 by Brij
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid