February 23, 2010

string.IsNullOrWhiteSpace()

NET 4 adds new method called string.IsNullOrWhiteSpace() which checks for spaces, empty or null. This is a nice time-saver for developers. This static method returns true if a string is full of whitespace characters. Let us consider the below example .

static void Main()
{
string strTest = "Simple Talk";
string strNull = null;
string strEmpty = string.Empty;
string strWhiteSpace = "\t\r\n\n ";
Console.WriteLine("Is null or whitespace Exmaple!!");
Console.WriteLine("TestSting: " + string.IsNullOrWhiteSpace(strTest));//false
Console.WriteLine("NullString: " + string.IsNullOrWhiteSpace(strNull)); //true
Console.WriteLine("EmptyString: " + string.IsNullOrWhiteSpace(strEmpty)); //true
Console.WriteLine("WhiteSpaceString: " + string.IsNullOrWhiteSpace(strWhiteSpace)); //true
Console.ReadLine();
}

No comments: