25
votes

I define a string and check it by string.IsNullOrEmptyOrWhiteSpace().

But I got this error:

'string' does not contain a definition for 'IsNullOrEmptyOrWhiteSpace' and no extension method 'IsNullOrEmptyOrWhiteSpace' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?) D:\project\project\Controllers\aController.cs 23 24 project

What is the reason?

7
@4thpage: there's no such thing as "csharp". It's "C#".John Saunders
If your using VS2010 its easy to switch target framework to 4.0 and like magic the error will disappearDuncan
@John Saunders: If it looks like csharp, smells like csharp, and is spelled like csharp, it must be csharp.AMissico
@AMi there is no csharp, c-sharp, c_#, or cshizzarp tag. There is a c# tag, which you can use to tag a c# question.user1228
You're looking for a function that isn't defined. string (System.String) only includes a definition for IsNullOrEmpty or IsNullOrWhitespace, not IsNullOrEmptyOrWhitespace. string.IsNullOrWhitespace will return true if the string is empty. Also, this is a question about the .NET framework and is not specific to c#.Bennor McCarthy

7 Answers

75
votes

String.IsNullOrWhiteSpace has been introduced in .NET 4. If you are not targeting .NET 4 you could easily write your own:

public static class StringExtensions
{
    public static bool IsNullOrWhiteSpace(string value)
    {
        if (value != null)
        {
            for (int i = 0; i < value.Length; i++)
            {
                if (!char.IsWhiteSpace(value[i]))
                {
                    return false;
                }
            }
        }
        return true;
    }
}

which could be used like this:

bool isNullOrWhiteSpace = StringExtensions.IsNullOrWhiteSpace("foo bar");

or as an extension method if you prefer:

public static class StringExtensions
{
    public static bool IsNullOrWhiteSpace(this string value)
    {
        if (value != null)
        {
            for (int i = 0; i < value.Length; i++)
            {
                if (!char.IsWhiteSpace(value[i]))
                {
                    return false;
                }
            }
        }
        return true;
    }
}

which allows you to use it directly:

bool isNullOrWhiteSpace = "foo bar".IsNullOrWhiteSpace();

For the extension method to work make sure that the namespace in which the StringExtensions static class has been defined is in scope.

9
votes
33
votes

Here's another alternative implementation, just for fun. It probably wouldn't perform as well as Darin's, but it's a nice example of LINQ:

public static class StringExtensions
{
    public static bool IsNullOrWhiteSpace(this string value)
    {
        return value == null || value.All(char.IsWhiteSpace);
    }
}
3
votes

I have used (in .NET v2.0):

public static class StringExtensions
{
    public static bool IsNullOrEmptyOrWhitespace(this string value)
    {
        return string.IsNullOrEmpty(value) || string.IsNullOrEmpty(value.Trim());
    }
}

The Trim() method will remove all leading or trailing whitespace so if your string is entirely whitespace, it will be reduced to the empty string.

I can't say that performance has been an issue.

2
votes

Exact copy from Microsoft's source code for .NET 4 Framework, ..\RefSrc\Source.Net\4.0\DEVDIV_TFS\Dev10\Releases\RTMRel\ndp\clr\src\BCL\System\String.cs\1305376\String.cs

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

Remarks

(from http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace.aspx)

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 the Char.IsWhiteSpace method as a white-space character.

2
votes

Funny enough nobody makes use of the Trim function here:

public static class StringExtensions
{
    public static bool IsNullOrEmptyOrWhiteSpace(this string value)
    {
        return string.IsNullOrEmpty(value) ||
               ReferenceEquals(value, null) ||
               string.IsNullOrEmpty(value.Trim(' '));
    }
}

Update: I see in the comments now it was proposed and rejected for various reasons, but there it is if one prefers brevity over efficiency...

0
votes

Pre .NET 4.0, the shortest:

public static bool IsNullOrWhiteSpace(this string value)
{
    return value == null || value.Trim() == "";
}

Not that efficient; Jon's is better considering readability and performance.