String.IsNullOrWhiteSpace in .NET Framework 4

By Mohammad Mahdi Ramezanpour at March 03, 2010 03:13
Filed Under: .NET General

Microsoft .NET New Logo 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.

Comments

5/24/2010 2:22:47 AM #

Generic 4 All Pharmacy Reviews

Many thanks for the post, I am fantastically enthralled and was thinking if someone had any other similar pages they could point me to. I enjoy making blog articles myself and would envy to gather as much information as is possible.

Generic 4 All Pharmacy Reviews United States

5/24/2010 6:42:59 AM #

music downlaods

Sorry if this this is a bit off subject for this tread, but everyone should think about this on this.  After I find a </A><A href="downloadable-mp3-music.org">downlaods</A> page I cant but wonder if these sites are legal.  I dont dare go to any type of torrent site.  How are Internet users suppose to know if the site that is allowing free downloads is safe.  

music downlaods United States

5/24/2010 6:45:04 AM #

paid online surveys

Good post & Wonderful blog! I would definitely love to get started in a blog too but I have no clue where to start. I possess the ability to do it (not that tough on the techie side) but I feel like I am too lazy to post on a regular basis... That is the problem, if you start you have to go all the way...

paid online surveys United States

5/25/2010 1:51:14 PM #

david

nice information bro...i like this blog..thanx Laughing

david U.A.E.

5/26/2010 6:18:20 PM #

registry cleaner comparison

Nice post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your articles. Anyway I'll be subscribing to your feed and I hope you post again soon.

registry cleaner comparison United States

5/27/2010 6:31:39 AM #

bulgaria

I like the blog, greetings from Bulgaria!

bulgaria United States

5/27/2010 8:57:14 AM #

bulgaria

I like the blog, greetings from Bulgaria!

bulgaria United States

5/28/2010 8:41:54 PM #

designer handbags

Hope to read more updates on your site soon!

designer handbags United States

5/29/2010 6:14:50 AM #

How to Duel Red Dead Redemption

Whats Up
Check out my video
http://www.youtube.com/watch?v=eCR8GCFjXVg

How to Duel Red Dead Redemption United States

5/30/2010 1:03:42 PM #

Gregory Despain

Between me and my husband we've owned more MP3 players over the years than I can count, including Sansas, iRivers, iPods (classic & touch), the Ibiza Rhapsody, etc. But, the last few years I've settled down to one line of players. Why? Because I was happy to discover how well-designed and fun to use the underappreciated (and widely mocked) Zunes are.

Gregory Despain United States

Comments are closed

Currently Reading

Quote of the day

Send Persian SMS