Detect an Empty String in .NET 4.0

In the previous versions of .NET, we used the String.IsNullOrEmpty method to find out whether the specified string is null or an Empty string. However this method did not tell us if the string contained only whitespaces. You had to use the Trim().Length method to detect that.

.NET 4.0 makes this simpler by introducing the String.IsNullOrWhiteSpace method to find out whether a specified string is null, empty, or consists only of white-space characters.

So the following piece of code

static void Main(string[] args)
string str = new String(' ', 10);

will return True.

About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

No comments: