Friday, March 4, 2011

KnowDotNet: String.IsNullOrWhiteSpace

So you don't care if a string has while space in it, all you want to do is ensure that a string doesn't have readable characters in it (or it's actually an empty string...

A simple example:
string myString = " ";
String.IsNullOrWhiteSpace( myString );

In the above example this returns true, as myString is not null and has only a space in it; no numbers or letters here!

Note that the IsNullOrWhileSpace() method is a static method on the String class.

Below is a gist containing several examples and their results. Next to the string variable declarations are comments showing what the code returns for the value of each string.

namespace String_IsNullOrWhitespace_Test
{
    using System;

    class Program
    {
        static void Main( string [] args ) {
            string test1 = null;            // True
            string test2 = String.Empty;    // True
            string test3 = "";              // True
            string test4 = " ";             // True
            string test5 = "\t";            // True
            string test6 = "\n";            // True
            string test7 = " a";            // False
            string test8 = "\0";            // False
            string test9 = "\b";            // False
            string test10 = "\a";           // False
           
            Console.WriteLine( String.IsNullOrWhiteSpace( test1 ) );
            Console.WriteLine( String.IsNullOrWhiteSpace( test2 ) );
            Console.WriteLine( String.IsNullOrWhiteSpace( test3 ) );
            Console.WriteLine( String.IsNullOrWhiteSpace( test4 ) );
            Console.WriteLine( String.IsNullOrWhiteSpace( test5 ) );
            Console.WriteLine( String.IsNullOrWhiteSpace( test6 ) );
            Console.WriteLine( String.IsNullOrWhiteSpace( test7 ) );
            Console.WriteLine( String.IsNullOrWhiteSpace( test8 ) );
            Console.WriteLine( String.IsNullOrWhiteSpace( test9 ) );
            Console.WriteLine( String.IsNullOrWhiteSpace( test10 ) );
        }
    }
}

Update: 2011-03-07 - Tarn Barford (@tarnacious) noted that this is a .Net 4 method. If you want similar functionality he mentioned that you can write an extension method... So for a bit more completeness here is one.

using System;
using System.Collections.Generic;
using System.Text;

namespace System
{
    public static class StringExtensionMethods
    {
        public static bool IsNullOrWhiteSpace(this string theString) {
            if ( theString != null ) {
                foreach ( char c in theString ) {
                    if ( Char.IsWhiteSpace( c ) == false )
                        return false;
                }
            }

            return true;
        }
    }
}

Then you can call the method like this:

Console.WriteLine( test1.IsNullOrWhiteSpace() );

Note that we can't make an equivalent static method using extension methods so it has to be used on a string reference.

No comments:

Post a Comment