开发者

IsNullOrEmptyOrWhiteSpace method missing

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?


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.


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


Maybe IsNullOrWhiteSpace is the method you are searching for? http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace.aspx


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.


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.


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...


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜