What is the difference between IsNullOrEmpty and IsNullOrWhiteSpace?

19

I have here that there is no practical difference between String.Empty and "" , and then it came to me doubt.

What's the difference between using String.IsNullOrEmpty(String) and String.IsNullOrWhiteSpace(String) ?

    
asked by anonymous 20.12.2016 / 22:30

2 answers

20

Both check if it is null, this seems obvious. Let's face the differences.

The first one only matters if the text size is zero, that is, if it has zero characters. So if you have a simple space it is no longer empty.

The runtime is constant - O (1).

The second parses character by character if each character is one of the characters considered a blank (it does not necessarily have to be a space, it can be tab, line break or other according to Unicode or Latin1 rules, the two encodings that he understands). If it has a non-white character it returns false and quits. If all characters are considered spaces, even if they have multiple, it returns true.

The runtime is linear O (N) in the worst case. If no character is not white, it will go up to the size of the text. If the first character is already non-white, it ends the search there and equals O (1) which is the best case.

Can be seen in the .Net source itself.

public static bool IsNullOrEmpty(String value) {
    return (value == null || value.Length == 0);
}

Font .

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;
}

Font .

It can be tested with this code:

using static System.Console;

public class Program {
    public static void Main() {
        string nullString = null;
        string emptyString = "";
        string spaceString = "    ";
        string tabString = "\t";
        string newlineString = "\n";
        string nonEmptyString = "texto";
        WriteLine(string.IsNullOrEmpty(nullString));
        WriteLine(string.IsNullOrEmpty(emptyString));
        WriteLine(string.IsNullOrEmpty(spaceString));
        WriteLine(string.IsNullOrEmpty(tabString));
        WriteLine(string.IsNullOrEmpty(newlineString));
        WriteLine(string.IsNullOrEmpty(nonEmptyString));
        WriteLine();
        WriteLine(string.IsNullOrWhiteSpace(nullString));
        WriteLine(string.IsNullOrWhiteSpace(emptyString));
        WriteLine(string.IsNullOrWhiteSpace(spaceString));
        WriteLine(string.IsNullOrWhiteSpace(tabString));
        WriteLine(string.IsNullOrWhiteSpace(newlineString));
        WriteLine(string.IsNullOrWhiteSpace(nonEmptyString));
    }
}

See running on dotNetFiddle and on CodingGround .

SO Docs .

    
20.12.2016 / 22:38
17

The string.IsNullOrEmpty is the same thing as:

result = s == null || s == String.Empty;

and string.IsNullOrWhiteSpace is the same thing as:

result = string.IsNullOrEmpty(s) || s.Trim().Length == 0;

that is, string.IsNullOrWhiteSpace has the checks for IsNullOrEmpty and Length == 0 .

According to dotnetperls site , the performance of string.IsNullOrWhiteSpace is not very good.

References:

20.12.2016 / 22:35