String.IsNullOrEmpty()检查空间 [英] String.IsNullOrEmpty() Check for Space

查看:122
本文介绍了String.IsNullOrEmpty()检查空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要什么让 String.IsNullOrEmpty()计空格的字符串作为空的?

What is needed to make String.IsNullOrEmpty() count whitespace strings as empty?

例如。我想下面的返回真正而不是通常的

Eg. I want the following to return true instead of the usual false:

String.IsNullOrEmpty(" ");



难道还有比一个更好的方法:

Is there a better approach than:

 String.IsNullOrEmpty(" ".Trim());

(注意,原来的问题问什么回报将是正常因而绝情的意见,这已经换成了更合理的问题)。

推荐答案

.NET 4.0将引入方法 String.IsNullOrWhiteSpace 。在那之前,你需要使用修剪如果你要处理的空白字符串你处理空字符串的方法相同。

.NET 4.0 will introduce the method String.IsNullOrWhiteSpace. Until then you'll need to use Trim if you want to deal with white space strings the same way you deal with empty strings.

对于不使用.NET 4.0的代码,一个辅助方法来检查或空或空白字符串可以这样实现的:

For code not using .NET 4.0, a helper method to check for null or empty or whitespace strings can be implemented like this:

public static bool IsNullOrWhiteSpace(string value)
{
    if (String.IsNullOrEmpty(value))
    {
        return true;
    }

    return String.IsNullOrEmpty(value.Trim());
}



String.IsNullOrEmpty 将不执行任何修整,将只检查是否字符串是一个空引用或空字符串。

The String.IsNullOrEmpty will not perform any trimming and will just check if the string is a null reference or an empty string.

这篇关于String.IsNullOrEmpty()检查空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆