繁体   English   中英

string.IsNullOrEmpty(myString.Trim()) 与 string.IsNullOrWhiteSpace(myString)

[英]string.IsNullOrEmpty(myString.Trim()) vs string.IsNullOrWhiteSpace(myString)

string.IsNullOrEmpty(myString.Trim())string.IsNullOrWhiteSpace(myString)

哪个更快或更可靠,为什么?

如果myStringnullstring.IsNullOrEmpty(myString.Trim())将抛出异常,而string.IsNullOrWhiteSpace(myString)将正常工作,因此它更可靠。

至于性能, string.IsNullOrWhiteSpace应该更快。

string.IsNullOrWhiteSpace(myString)是检查变量是空还是空格的首选方法。

IsNullOrWhiteSpace是一种方便的方法,类似于以下代码,只是它提供了卓越的性能:

return String.IsNullOrEmpty(value) || value.Trim().Length == 0;

可靠性的唯一区别是myString.Trim()可能抛出NullReferenceException

从性能角度来看, Trim是决定性因素。 注意在Trim的情况下,如何从每一端迭代字符串。 在某些情况下,这可能特别昂贵,正如@Lukazoid所指出的那样。 IsNullOrWhiteSpace将从头开始,只遍历字符串,直到找到非空白字符。 下面是.NET源代码。

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

    [Pure]
    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; 
    }

    // Trims the whitespace from both ends of the string.  Whitespace is defined by
    // Char.IsWhiteSpace. 
    // 
    [Pure]
    public String Trim() { 
        Contract.Ensures(Contract.Result<String>() != null);
        Contract.EndContractBlock();

        return TrimHelper(TrimBoth); 
    }

    [System.Security.SecuritySafeCritical]  // auto-generated
    private String TrimHelper(int trimType) { 
        //end will point to the first non-trimmed character on the right
        //start will point to the first non-trimmed character on the Left
        int end = this.Length-1;
        int start=0; 

        //Trim specified characters. 
        if (trimType !=TrimTail)  { 
            for (start=0; start < this.Length; start++) {
                if (!Char.IsWhiteSpace(this[start])) break; 
            }
        }

        if (trimType !=TrimHead) { 
            for (end= Length -1; end >= start;  end--) {
                if (!Char.IsWhiteSpace(this[end])) break; 
            } 
        }

        return CreateTrimmedString(start, end);
    }

string.IsNullOrWhiteSpace(myString)更可靠,因为当myString为null时,它不会引发NullReferenceException。 我相信IsNullOrWhiteSpace(myString)比myString.Trim()快,想到一个字符串,两端包含1个空格,中间包含300万个其他字符。 在检查之前,必须将这三百万个字符复制到一个新字符串。 IsNullOrWhiteSpace必须比较两个字符。

String.IsNullOrWhiteSpace()将更可靠,更快捷。

更可靠,因为它正确处理null。 而且速度更快,因为它不需要创建新的字符串。

如果你真的想在优化方面走得那么远, string.IsNullOrWhiteSpace(myString)会有更好的性能,因为它能够立即返回结果。

请使用以下字符串:

" B C    " (4 trailing spaces)

使用string.IsNullOrEmpty(myString.Trim())

  1. 修剪字符串,迭代超过5个字符(前1个和4个尾随空格),产生“BC”
  2. IsNullOrEmpty迭代1个字符并返回false。

共检查了6个字符。

使用string.IsNullOrWhitespace(myString)

  1. 迭代超过2个字符,在第二个字符上返回false

共检查了2个字符。

尾随空格的数量越大, string.IsNullOrWhitespace(myString)益处越大string.IsNullOrWhitespace(myString)将提供替代方案。

作为其他答案和注释中的状态, Trim()的附加字符串结果的实例化会增加更多开销。

这取决于您的应用程序,但您必须小心转义字符。 这里我们考虑String.IsNullOrEmpty

String.IsNullOrEmpty(""); //True
String.IsNullOrEmpty(null); //True
String.IsNullOrEmpty("   "); //False
String.IsNullOrEmpty("\n"); //False
String.IsNullOrEmpty("\t"); //False
String.IsNullOrEmpty("hello"); //False

现在String.IsNullOrWhiteSpace

String.IsNullOrWhiteSpace("");//True
String.IsNullOrWhiteSpace(null);//True
String.IsNullOrWhiteSpace("   ");//True
String.IsNullOrWhiteSpace("\n");//True
String.IsNullOrWhiteSpace("\t");//True
String.IsNullOrWhiteSpace("hello");//False

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM