用哪种方法判断字符串为空更快速
三种常用的字符串判空串方法:
- Length法:bool isEmpty = (str.Length == 0);
- Empty法:bool isEmpty = (str == String.Empty);
- General法:bool isEmpty = (str == "");
实践证明用Length方法判空是最快的(在32位系统上,System.Int32运算最快了),注意只有字符串不为null的前提下,.NET才会进一步计算s.Length是否为0,所以一般常用 if( (s == null) || (s.Length == 0) ) 进行判断。
博文作者:mephisto
本文版权归作者和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作!
如果阅读了本文章,觉得有帮助,您可以选择捐助我: