What is the difference between Isnullorempty and Isnullorwhitespace?

Asked

Viewed 5,046 times

19

Li here that there is no practical difference between String.Empty and "", and then the doubt came to me.

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

2 answers

24


Both check if it is null, this seems obvious. Let’s get to the differences.

At first it only matters if the size of the text is zero, ie if it has zero characters. So if you have a single space is no longer empty.

Running time is constant - O(1).

The second analyzes character by character if each of them is one of the characters considered a white (it does not necessarily have to be a space, it can be tab, line break or other according to the rules of Unicode or Latin1, the two encodings that he understands). If you have a character that is not white it returns false and closes. If all characters are considered spaces, even if there are several, returns true.

Run time is linear O(n) in the worst case. If no character is non-white, it will go to the size of the text. If the first character is already non-white, it closes the search right there and will equal O(1) which is the best case.

Can be seen in the source itself of . NET.

public static bool IsNullOrEmpty(String value) => (value == null || value.Length == 0);

Source. In the .NET 5 onwards.

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

Source. In the .NET 5 onwards.

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

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • Doubt. Looking at the source of c#, it is a common static method. How does it identify that it is possible to use this in strings? I say this because if it was an extension it would be easier to understand...

  • In fact I find it easier (even if it’s almost the same) to identify like this, either for the compiler or for a human. I even get in doubt what doubt I would have, because explicitly it is calling through the class string. otherwise the parameter is of type string.

  • Right, but when we have a literal "" is parsed for string class?

  • That’s a given of a kind string. You can see it in the answer code.

  • I’m confusing things. Actually the question is: Why do we have to use string.IsNullOrEmpty("") or instead of "".IsNullOrEmpty()? Function style ToUpper.

  • 3

    This is very interesting. Not for the literal, this will always work, but the most with is not using a literal, because if you use you already know the answer just by looking at the code, you don’t need to process anything. But with a variable the object can be null and would break the application trying to access an object by its null value, then the static method always works, you call the method and pass a value that is string including a null is possible. Static method is an independent function, instance method depends on the instance exist, and the null value indicates that it does not exist.

Show 1 more comment

18

The string.IsNullOrEmpty is the same thing as:

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

and the string.IsNullOrWhiteSpace is the same thing as:

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

i.e., the string.IsNullOrWhiteSpace has the checks of IsNullOrEmpty and Length == 0.

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

References:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.