C# string.IsNullOrEmpty(myString.Trim())与string.IsNullOrWhiteSpace(myString)
C# string.IsNullOrEmpty(myString.Trim())与string.IsNullOrWhiteSpace(myString),c#,.net,visual-studio-2010,visual-studio,C#,.net,Visual Studio 2010,Visual Studio,string.IsNullOrEmpty(myString.Trim())vsstring.IsNullOrWhiteSpace(myString) 哪一个更快或更可靠?为什么?string.IsNullOrEmpty(myString.Trim())将在myString为null时引发异常,而string.IsNullOrWhiteSpace(myString)将正常工作,因此更可靠 至于性能,string.IsNullOrWhiteSpace应该更快 string.IsNullOrWhite
string.IsNullOrEmpty(myString.Trim())
vsstring.IsNullOrWhiteSpace(myString)
哪一个更快或更可靠?为什么?string.IsNullOrEmpty(myString.Trim())
将在myString
为null
时引发异常,而string.IsNullOrWhiteSpace(myString)
将正常工作,因此更可靠
至于性能,string.IsNullOrWhiteSpace
应该更快
string.IsNullOrWhiteSpace(myString)
是检查变量是空还是空白的首选方法。string.IsNullOrWhiteSpace(myString)更可靠,因为当myString为null时,它不会引发NullReferenceException。
我相信IsNullOrWhiteSpace(MyString)比MyString。Times()更快,在两端包含一个包含1个空间和三百万个中间字符的字符串。在检查之前,必须将这300万个字符复制到一个新字符串中。IsNullOrWhiteSpace必须比较两个字符。
String。IsNullOrWhiteSpace()
将更可靠、更快
更可靠,因为它正确地处理null。更快,因为它不需要创建新字符串。如果您真的想在优化方面走这么远,
string.IsNullOrWhiteSpace(myString)
将具有更好的性能,因为它能够立即返回结果
以以下字符串为例:
" B C " (4 trailing spaces)
使用string.IsNullOrEmpty(myString.Trim())
:
string.IsNullOrWhitespace(myString)
:
string.IsNullOrWhitespace(myString)
提供的好处就越大
正如其他答案和注释中所述,Trim()
对附加字符串的实例化增加了更多开销
是一种方便的方法,类似于
除了提供优异的性能外,以下代码:
可靠性方面唯一的区别是myString.Trim()
可能会抛出错误
从性能的角度来看,是决定因素。注意字符串是如何从每一端迭代的。正如@Lukazoid所指出的,这在某些情况下可能特别昂贵。将从头开始,并仅在找到非空白字符之前遍历字符串。下面是.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);
}
publicstaticbool为空或空(字符串值){
返回值(value==null | | value.Length==0);
}
[纯]
公共静态bool IsNullOrWhiteSpace(字符串值){
if(value==null)返回true;
for(int i=0;i=开始;结束--){
如果(!Char.IsWhiteSpace(this[end])中断;
}
}
返回CreateTrimmedString(开始、结束);
}
完全猜测(没有任何支持),我会说IsNullOrWhiteSpace。这有关系吗?“过早优化是万恶之源”同样,在这种背景下定义“可靠”;我在Reflector中查看了这一点,IsNullOrWhiteSpace
在String
命名空间中的显示方式与MSDN文档中的显示方式不同。我想知道为什么会是这样。@YYY因为它是后来在.NET Framework v4.0中引入的。Trim()从开始到结束进行迭代。真正的区别在于它必须实例化一个新的string@Tar我没有想到,谢谢你通知我,我真的应该用反射器。我将更新我的答案以纳入此知识。如果您通过参考源网站访问Microsoft.NET framework源代码,它会说IsNullOrWhiteSpace
较慢。IsNullOrWhiteSpace()
比IsNullOrEmpty()
较慢。然而,最初的问题是将它与string.IsNullOrEmpty(myString.Trim())
进行比较。调用Trim()
必须先创建一个新字符串,并从原始字符串复制字符,然后才能对其进行测试。总之,我很确定IsNullOrWhiteSpace()
会更快。然后我想问string.IsNullOrEmpty(myString.Trim())
vsstring.IsNullOrEmpty(myString?.Trim())
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);
}