Difference between IsNullOrEmpty and IsNullOrWhiteSpace in C #

What are the differences between these commands in C #

string text= " "; 1-string.IsNullOrEmpty(text.Trim()) 2-string.IsNullOrWhiteSpace(text) 
+58
string c # difference string-function
Sep 10 '13 at 4:18
source share
7 answers

IsNullOrWhiteSpace is a convenience method similar to the following code, except that it provides excellent performance:

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

White space characters are defined by the Unicode standard. IsNullOrWhiteSpace method interprets any character that returns true when it is passed to the Char.IsWhiteSpace method as a white space.

+79
Sep 10 '13 at 4:21
source share

The first method checks if the string is empty or empty. 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 the string is null or an arbitrary number of spaces in the string (including an empty string)

 2- string .IsNullOrWhiteSpace(text) 

The IsNullOrWhiteSpace method covers IsNullOrEmpty , but also returns true if the string contains a space.

In your specific example, you should use 2), because you run the risk of excluding the reference reference position in approach 1), since you are causing the line to be truncated, which may be null

+18
Sep 10 '13 at 4:21
source share

space and tab - difference

 string.IsNullOrWhiteSpace("\t"); //true string.IsNullOrEmpty("\t"); //false string.IsNullOrWhiteSpace(" "); //true string.IsNullOrEmpty(" "); //false 
+12
Jul 01 '14 at 2:56
source share

String.IsNullOrEmpty(string value) returns true if the string is empty or empty. For reference, an empty string is represented by a "" (two double-quote characters)

String.IsNullOrWhitespace(string value) returns true if the string is empty, empty, or contains only white space characters, such as a space or tab.

To see which characters are considered spaces, consult this link: http://msdn.microsoft.com/en-us/library/t809ektx.aspx

+5
Sep 10 '13 at 4:24
source share

If your line (in your case the text variable) can be zero, this will lead to a big difference:

1- string.IsNullOrEmpty(text.Trim()) -> EXCEPTION , since your call to mthode is a null object

2- string.IsNullOrWhiteSpace(text) This will work fine, since the null issue is checked internally.

To ensure the same behavior using option 1, you will need to somehow check if it is not null first, and then use the trim () method

 if ((text != null) && string.IsNullOrEmpty(text.Trim())) { ... } 
+3
Oct 24 '16 at 9:49
source share

The actual implementation of the methods looks.

  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, apparently, the second method also checks white spaces not only for the length of the input string. Spaces: https://msdn.microsoft.com/en-us/library/system.char.iswhitespace(v=vs.110).aspx

+3
Oct 24 '16 at 10:09
source share

string.isNullOrWhiteSpace (text) should be used in most cases as , as well as an empty string .

 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { //Your code goes here var str = ""; Console.WriteLine(string.IsNullOrWhiteSpace(str)); } } } 

It returns True !

0
May 23 '17 at 8:16
source share



All Articles