String.IsNullOrEmpty (myString.Trim ()) vs string.IsNullOrWhiteSpace (myString)

string.IsNullOrEmpty(myString.Trim()) vs string.IsNullOrWhiteSpace(myString)

Which one is faster or more reliable and why?

+4
source share
5 answers

string.IsNullOrEmpty(myString.Trim()) will throw an exception if myString is null , while string.IsNullOrWhiteSpace(myString) will work fine, so it is more reliable.

In terms of performance, string.IsNullOrWhiteSpace should be faster.

string.IsNullOrWhiteSpace(myString) preferable to check if the variable is empty or whitespace.

+11
source

string.IsNullOrWhiteSpace (myString) is more reliable because it will not throw a NullReferenceException when myString is null. I believe IsNullOrWhiteSpace (myString) is faster than myString.Trim (), think of a line containing 1 space at both ends and three million other characters in the middle. These three million characters must be copied to a new line before verification. IsNullOrWhiteSpace will have to compare two characters.

+1
source

String.IsNullOrWhiteSpace() will be more reliable and faster.

More reliable as it handles zero correctly. And faster, because he does not need to create a new line.

+1
source

If you really want to do this in terms of optimization, string.IsNullOrWhiteSpace(myString) will have better performance, as it will be able to immediately return the result.

Take the following line:

 " BC " (4 trailing spaces) 

With string.IsNullOrEmpty(myString.Trim()) :

  • Trim string, iterate over 5 characters (1 previous and 4 trailing spaces), resulting in "BC"
  • IsNullOrEmpty iterates 1 character and returns false.

A total of 6 characters are marked.

With string.IsNullOrWhiteSpace(myString) :

  • Iterate over two characters, returns false for the second character

Only 2 characters checked.

The greater the number of string.IsNullOrWhiteSpace(myString) spaces, the greater the benefits of string.IsNullOrWhiteSpace(myString) will be provided over the alternative.

As states in other answers and comments, creating an additional row result from Trim() adds extra overhead.

+1
source

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

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

The only difference in reliability is that myString.Trim() can raise a NullReferenceException .

In terms of performance, Trim is a decisive factor. Note that in the case of Trim, the line is repeated from each end. This can be especially costly in some cases, as noted by Lucasoid. IsNullOrWhiteSpace will start from the very beginning and will only go through the line until a character with no spaces is found. Below is a .NET source.

  public static bool IsNullOrEmpty(String value) { return (value == null || value.Length == 0); } [Pure] 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; } // Trims the whitespace from both ends of the string. Whitespace is defined by // Char.IsWhiteSpace. // [Pure] public String Trim() { Contract.Ensures(Contract.Result<String>() != null); Contract.EndContractBlock(); return TrimHelper(TrimBoth); } [System.Security.SecuritySafeCritical] // auto-generated private String TrimHelper(int trimType) { //end will point to the first non-trimmed character on the right //start will point to the first non-trimmed character on the Left int end = this.Length-1; int start=0; //Trim specified characters. if (trimType !=TrimTail) { for (start=0; start < this.Length; start++) { if (!Char.IsWhiteSpace(this[start])) break; } } if (trimType !=TrimHead) { for (end= Length -1; end >= start; end--) { if (!Char.IsWhiteSpace(this[end])) break; } } return CreateTrimmedString(start, end); } 
+1
source

All Articles