Difference between IsNullOrEmpty and IsNullOrWhiteSpace in C#
Solution 1
IsNullOrWhiteSpace
is a convenience method that is similar to the following code, except that it offers superior performance:return String.IsNullOrEmpty(value) || value.Trim().Length == 0;
White-space characters are defined by the Unicode standard. The
IsNullOrWhiteSpace
method interprets any character that returns a value of true when it is passed to theChar.IsWhiteSpace
method as a white-space character.
Solution 2
Short answer:
In common use, space " "
, Tab "\t"
and newline "\n"
are the difference:
string.IsNullOrWhiteSpace("\t"); //true
string.IsNullOrEmpty("\t"); //false
string.IsNullOrWhiteSpace(" "); //true
string.IsNullOrEmpty(" "); //false
string.IsNullOrWhiteSpace("\n"); //true
string.IsNullOrEmpty("\n"); //false
https://dotnetfiddle.net/4hkpKM
also see this answer about: whitespace characters
Long answer:
There are also a few other white space characters, you probably never used before
- Members of the UnicodeCategory.SpaceSeparator category, which includes the characters
SPACE (U+0020), NO-BREAK SPACE (U+00A0), OGHAM SPACE MARK (U+1680), EN QUAD (U+2000), EM QUAD (U+2001), EN SPACE (U+2002), EM SPACE (U+2003), THREE-PER-EM SPACE (U+2004), FOUR-PER-EM SPACE (U+2005), SIX-PER-EM SPACE (U+2006), FIGURE SPACE (U+2007), PUNCTUATION SPACE (U+2008), THIN SPACE (U+2009), HAIR SPACE (U+200A), NARROW NO-BREAK SPACE (U+202F), MEDIUM MATHEMATICAL SPACE (U+205F), and IDEOGRAPHIC SPACE (U+3000)
. - Members of the UnicodeCategory.LineSeparator category, which consists solely of the
LINE SEPARATOR character (U+2028)
. - Members of the UnicodeCategory.ParagraphSeparator category, which consists solely of the
PARAGRAPH SEPARATOR character (U+2029)
. - The characters
CHARACTER TABULATION (U+0009), LINE FEED (U+000A), LINE TABULATION (U+000B), FORM FEED (U+000C), CARRIAGE RETURN (U+000D), and NEXT LINE (U+0085)
.
https://docs.microsoft.com/en-us/dotnet/api/system.char.iswhitespace
Solution 3
The first method checks if a string is null or a blank string. In your example you can risk a null reference since you are not checking for null before trimming
1- string.IsNullOrEmpty(text.Trim())
The second method checks if a string is null or an arbitrary number of spaces in the string (including a blank string)
2- string .IsNullOrWhiteSpace(text)
The method IsNullOrWhiteSpace
covers IsNullOrEmpty
, but it also returns true
if the string contains only white space characters.
In your concrete example you should use 2) as you run the risk of a null reference exception in approach 1) since you're calling trim on a string that may be null
Solution 4
This is implementation of methods after decompiling.
public static bool IsNullOrEmpty(String value)
{
return (value == null || value.Length == 0);
}
public static bool IsNullOrWhiteSpace(String value)
{
if (value == null) return true;
for(int i = 0; i < value.Length; i++) {
if(!Char.IsWhiteSpace(value[i])) return false;
}
return true;
}
So it is obvious that IsNullOrWhiteSpace method also checks if value that is being passed contain white spaces.
Whitespaces refer : https://msdn.microsoft.com/en-us/library/system.char.iswhitespace(v=vs.110).aspx
Solution 5
String.IsNullOrEmpty(string value)
returns true
if the string is null or empty.
For reference an empty string is represented by "" (two double quote characters)
String.IsNullOrWhitespace(string value)
returns true
if the string is null, empty, or contains only whitespace characters such as a space or tab.
To see what characters count as whitespace consult this link: http://msdn.microsoft.com/en-us/library/t809ektx.aspx
Related videos on Youtube
![Asieh hojatoleslami](https://i.stack.imgur.com/x7JnX.jpg?s=256&g=1)
Asieh hojatoleslami
programmer, web developer, System analyst
Updated on December 07, 2021Comments
-
Asieh hojatoleslami over 2 years
What are differences between these commands in C#
string text= " "; 1-string.IsNullOrEmpty(text.Trim()) 2-string.IsNullOrWhiteSpace(text)
-
ojlovecd almost 11 years
-
samus almost 7 years"
IsNullOrWhiteSpace
coversIsNullOrEmpty
" (see TGH answer).
-
-
samus almost 7 yearsWhat about
string.IsNullOrWhiteSpace("")
? -
fubo almost 7 years@samusarin no difference dotnetfiddle.net/9uWpvh
-
Azimuth over 5 yearsYour example is not correct since
string.IsNullOrEmpty
will return true for variablestr
. -
vibs2006 over 5 years@Azimuth I'm not able to understand properly. Can you please create a fiddle on rextester.com or any other C# online Fiddle Tool that you prefer?
-
Azimuth over 5 yearswhy should I provide some fiddle? Just replace
IsNullOrWhiteSpace
withIsNullOrEmpty
in your code and run it. It will returntrue
. Thus, your example doesn't show the difference betweenIsNullOrWhiteSpace
andIsNullOrEmpty
. -
vibs2006 over 5 yearssure I'll do that. Thanks @Azimuth for explaining.
-
Sachintha Udara over 5 yearsstring.IsNullOrWhiteSpace returns this expression String.IsNullOrEmpty(value) || value.Trim().Length == 0; So, use it in textbox of entry(xamarin) validation handlers
-
goldenratio about 5 yearswith c#6 you can do
string.IsNullOrEmpty(text?.Trim())
safely -
Shaiju T over 3 yearsSo its always better to use
IsNullOrWhiteSpace
thanIsNullOrEmpty
? -
Shaiju T over 3 yearsNice, : ) How did you decompile to look at the implementation ?
-
Ľuboš Pilka over 3 yearsUsing decompiler like dotpeek
-
galaxis almost 3 yearsActually the trimmed string length test will also catch empty strings, so