Scott Dorman

ephemeral segment

  Home  |   Contact  |   Syndication    |   Login
  603 Posts | 10 Stories | 862 Comments | 51 Trackbacks

News


Post Categories

Image Galleries


Microsoft Store


Creative Commons License



Locations of visitors to this page

Subscribers to this feed

TwitterCounter for @sdorman

View blog authority

Add to Technorati Favorites

Windows Live Alerts

AddThis Social Bookmark Button

LinkedIn profile

Community Credit profile

The Code Project

Follow me on Twitter

Get Free Shots from Snap.com

Community Credit Hall of Fame

Get Feedghost

Xobni outlook add-in for your inbox



Support This Site

Tag Cloud


Article Categories

Archives

Post Categories

Image Galleries

An empty string is different than an unassigned string variable (which is null), and is a string containing no characters between the quotes (""). The .NET Framework provides String.Empty to represent an empty string, and there is no practical difference between ("") and String.Empty.

One of the most common string comparisons to perform is to determine if a string variable is equal to an empty string. The fastest and simplest way to determine if a string is empty is to test if the Length property is equal to 0. However, since strings are reference types it is possible for a string variable to be null, which would result in a runtime error when you tried to access the Length property.

Since testing to determine if a string is empty is such a common occurrence, the .NET Framework provides the static method String.IsNullOrEmpty method:

public static bool IsNullOrEmpty(string value)
{
    if (value != null)
    {
        return (value.Length == 0);
    }
 
    return true;
}

It is also very common to determine if a string is empty and contains more than just whitespace characters. For example, String.IsNullOrEmpty("   ") would return false, since this string is actually made up of three whitespace characters. In some cases, this may be acceptable, but in many others it is not. TO help simplify testing this scenario, the .NET Framework 4 introduces the String.IsNullOrWhiteSpace method:

public static bool IsNullOrWhiteSpace(string value)
{
    if (value != null)
    {
        for (int i = 0; i < value.Length; i++)
        {
            if (!char.IsWhiteSpace(value[i]))
            {
                return false;
            }
        }
    }
    
    return true;
}
 

Using either String.IsNullOrEmpty or String.IsNullOrWhiteSpace helps ensure correctness, readability, and consistency, so they should be used in all situations where you need to determine if a string is null, empty, or contains only whitespace characters.

Technorati Tags: ,
Digg This
posted on Saturday, March 13, 2010 1:10 PM

Feedback

# re: String.IsNullOrWhiteSpace 3/13/2010 2:31 PM Denis Gladkikh
public static bool IsNullOrWhiteSpace(string value)
{
if (value != null)
{
return value.Trim().Lenght == 0;
}
return true;
}

And you can do extension methods like
public static bool IsNullOrWhiteSpace(this string value)
{
...
}

# re: String.IsNullOrWhiteSpace 3/16/2010 3:42 AM Anders Dalvander
@Denis: Using `string.Trim` instead of `char.IsWhiteSpace` could lead to performance issues in the long run as a new string is created. Using `char.IsWhiteSpace` is not a premature optimization, as the `string.IsNullOrWhiteSpace` exist in the base library, but using `string.Trim` is a premature pessimisation.

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: