Comparing String.IsNullOrWhiteSpace and String.IsNullOrEmpty

IsNullOrEmpty method provides us with a good and relatively efficient method of checking whether a string is null or empty. For performance it may be better to use manual null checks. IsNullOrWhiteSpace is not ideal but is good. The method calls result in a small performance penalty. Further, the IsWhiteSpace method itself has some indirections that can be removed if you are not using Unicode data.

String.IsNullOrEmpty

This function is used to check whether the string is null or empty. IsNullOrEmpty is a convenience method that enables you to simultaneously test whether a String is null or its value is Empty. It is equivalent to the following code:

bool result = stringVariable == null || stringVariable == String.Empty;

Lets see the following example to understand the usage of String.IsNullOrEmpty.

using System;

namespace IsNullOrEmptyIsNullOrWhiteSpace
{
    class Program
    {
        static void Main(string[] args)
        {
            string s1 = "hello";
            string s2 = "";
            string s3 = "       ";
            string s4 = null;
            string s5 = Environment.NewLine;

            Console.WriteLine("String s1 {0}.", Test(s1));
            Console.WriteLine("String s2 {0}.", Test(s2));
            Console.WriteLine("String s3 {0}.", Test(s3));
            Console.WriteLine("String s4 {0}.", Test(s4));
            Console.WriteLine("String s4 {0}.", Test(s5));

            Console.ReadKey();
        }

        public static String Test(string s)
        {
            if (String.IsNullOrEmpty(s))
                return "is null or empty";
            else
                return String.Format("(\"{0}\") is neither null nor empty", s);
        }
    }
}

Output as below,

In the above example, variable s3 and s5 returned false for String.IsNullOrEmpty().

You can use the IsNullOrWhiteSpace method to test whether a string is null, its value is String.Empty, or it consists only of white-space characters.

String.IsNullOrWhiteSpace

This method is used to check whether a specified string is null, empty, or consists only of white-space characters. IsNullOrWhiteSpace is a convenience method that is similar to the following code, except that it offers superior performance:

return String.IsNullOrEmpty(stringVariable) || stringVariable.Trim().Length == 0;

Lets see the following example to understand the usage of String.IsNullOrWhiteSpace.

using System;

namespace IsNullOrEmptyIsNullOrWhiteSpace
{
    class Program
    {
        static void Main(string[] args)
        {
            string s1 = "hello";
            string s2 = "";
            string s3 = "       ";
            string s4 = null;
            string s5 = Environment.NewLine;
            string s6 = "\t";

            Console.WriteLine("String.IsNullOrWhiteSpace(s1) : {0}.", Test(s1));
            Console.WriteLine("String.IsNullOrWhiteSpace(s2) : {0}.", Test(s2));
            Console.WriteLine("String.IsNullOrWhiteSpace(s3) : {0}.", Test(s3));
            Console.WriteLine("String.IsNullOrWhiteSpace(s4) : {0}.", Test(s4));
            Console.WriteLine("String.IsNullOrWhiteSpace(s5) : {0}.", Test(s5));
            Console.WriteLine("String.IsNullOrWhiteSpace(s6) : {0}.", Test(s6));

            Console.ReadKey();
        }

        public static String Test(string s)
        {
            return String.IsNullOrWhiteSpace(s).ToString();
        }
    }
}

Output as below,

Except s1 all other variables returned True for IsNullOrWhiteSpace.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s