As you know, String class in .NET Framework contains lots of methods and properties that related to strings. One of the popular methods that we had in previous versions of .NET (3.5, 2.0, etc.) was String.IsNullOrEmpty that indicates whether a String variable contains a string or it’s Null (Nothing in VB). I’m using this method a lot of time in my project it becomes one of my favorite methods in .NET.
But what if you want to check if a String variable is Null or is contains White spaces! It’s something that I wanted to explain in this post.
In Visual Studio 2010, Microsoft introduced a new method in the String class. The method is String.IsNullOrWhiteSpace that Indicates whether a specified string is null reference (Nothing in Visual Basic), empty, or consists only of white-space characters.
How it works:
The following code shows you how to use String.IsNullOrWhiteSpace method:
using System;
public class Example
{
public static void Main()
{
string[] values = { null, String.Empty, "ABCDE",
new String(' ', 20), " \t ",
new String('\u2000', 10) };
foreach (string value in values)
Console.WriteLine(String.IsNullOrWhiteSpace(value));
}
}
// The example displays the following output:
// True
// True
// False
// True
// True
// True
For more information please visit: http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace%28VS.100%29.aspx
Hope it helps.
c9070fb9-8113-4555-aa32-1f2e2da4be43|0|.0