DotNet Mirror
  DNM facebook   DNM Google+   DNM Twitter   

Difference between string.IsNullOrWhiteSpace and string.IsNullOrEmpty

By Ashok Nalam on 19 Nov 2013 | Category: C# | Tagged: string .NET difference 
The snippet covers difference between string.IsNullOrWhiteSpace and string.IsNullOrEmpty using an example.
  Discuss Add Comment   |  
Add rating Rate this resource   
Most of the times we use string.IsNullOrEmpty and sometimes string.IsNullOrWhiteSpace. The method names itself are self explanatory but to understand them better we will see an example.

Function used to check whether string is null or empty. 
String.IsNullOrEmpty = (Variable == null || Variable == string.Empty)

Function used to check whether a string is null, or contains only of white-space characters, or string.Empty
String.IsNullOrWhiteSpace = (string.IsNullOrEmpty(Variable) || Variable.Trim().Length == 0)

namespace DotNetMirror
    class StringIsNullOrWhiteSpaceAndStringIsNullOrEmpty
        static void Main(string[] args)
            string strVar1 = null;
            string strVar2 = "       ";
            string strVar3 = "    dnm   ";
            string strVar4 = "";
            string strVar5 = "dotnetmirror";

            #region IsNullOrWhiteSpace
            Console.WriteLine("IsNullOrWhiteSpace method");
            Console.WriteLine("String strVar1 {0}", CheckIsNullOrWhiteSpace(strVar1));
            Console.WriteLine("String strVar2 {0}", CheckIsNullOrWhiteSpace(strVar2));
            Console.WriteLine("String strVar3 {0}", CheckIsNullOrWhiteSpace(strVar3));
            Console.WriteLine("String strVar4 {0}", CheckIsNullOrWhiteSpace(strVar4));
            Console.WriteLine("String strVar5 {0}", CheckIsNullOrWhiteSpace(strVar5));
            #region IsNullOrEmpty
            Console.WriteLine("IsNullOrEmpty method");
            Console.WriteLine("String strVar1 {0}", CheckIsNullOrEmpty(strVar1));
            Console.WriteLine("String strVar2 {0}", CheckIsNullOrEmpty(strVar2));
            Console.WriteLine("String strVar3 {0}", CheckIsNullOrEmpty(strVar3));
            Console.WriteLine("String strVar4 {0}", CheckIsNullOrEmpty(strVar4));
            Console.WriteLine("String strVar5 {0}", CheckIsNullOrEmpty(strVar5));


        public static string CheckIsNullOrWhiteSpace(string str)
            if (string.IsNullOrWhiteSpace(str))
                return string.Format("[{0}] is Null or Empty or White-space.", str);   
                return string.Format("[{0}] is NOT Null or Empty or White-space.", str);
        public static string CheckIsNullOrEmpty(string str)
            if (string.IsNullOrEmpty(str))
                return string.Format("[{0}] is Null or Empty.", str);   
                return string.Format("[{0}] is NOT Null or Empty.", str);


String.IsNullOrWhiteSpace vs String.IsNullOrEmpty
  Discuss Add Comment    
Add rating Rate this resource   
About the Contributor
Member Since : 10 Dec 2012
Member Points (Level) : 9226  (Professional)
Location : INDIA
Home Page :
About : I am admin of this site.
Rate this resource
Add your Comment
Name Email WebSite
Captcha Refresh

Comments (0)
No comments found, click here to add comment.
cheap jordans|wholesale air max|wholesale jordans|wholesale jewelry|wholesale jerseys