Monday, March 22, 2010

C# 4.0-string.IsNullOrWhiteSpace()

We already have string.IsNullOrEmpty() method to check whether the string is null or empty.Now with Microsoft.NET Framework 4.0 there is a new another method that is called string.IsNullOrWhiteSpace() which will also check whether string is having whitespaces or not with null and empty string.This method is help full where we can check string is having whitespace rather then its empty or null. It will return a true if a string is empty,null or its having whitespaces. Let’s create a simple example and will see how its works. Here we are going test it with 4 options Empty string,Normal String,White spaces and Null String. Following is a code for that.

class Program
static void Main(string[] args)
string TestString = string.Empty;
Console.WriteLine("Test with empty string:{0}",
TestString = "Normal String";
Console.WriteLine("Test with normal string:{0}",
TestString = " ";
Console.WriteLine("Test with whitespace:{0}",
TestString = null;
Console.WriteLine("Test with null:{0}",
Following is a output of the above code here you can see its returning false with Normal string and for all other options its returning true as expected.

Shout it
kick it on


Post a Comment

Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog


My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews