在.NET 4.0及更高版本中,string.IsNullOrEmpty(string)
当检查被视为不良操作的字符串时是否使用string.IsNullOrWhiteSpace(string)
?
Cha*_*thJ 314
最佳做法是选择最合适的一种.
.Net Framework 4.0 Beta 2为字符串提供了一个新的IsNullOrWhiteSpace()方法,它将IsNullOrEmpty()方法概括为除了空字符串之外还包括其他空格.
术语"空白区域"包括屏幕上不可见的所有字符.例如,空格,换行符,制表符和空字符串是空格字符*.
参考:这里
对于性能,IsNullOrWhiteSpace并不理想但是很好.方法调用将导致较小的性能损失.此外,IsWhiteSpace方法本身具有一些可以在不使用Unicode数据时删除的间接.与往常一样,过早优化可能是邪恶的,但它也很有趣.
参考:这里
检查源代码(参考源.NET Framework 4.6.2)
[Pure]
public static bool IsNullOrEmpty(String value) {
return (value == null || value.Length == 0);
}
Run Code Online (Sandbox Code Playgroud)
[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;
}
Run Code Online (Sandbox Code Playgroud)
例子
string nullString = null;
string emptyString = "";
string whitespaceString = " ";
string nonEmptyString = "abc123";
bool result;
result = String.IsNullOrEmpty(nullString); // true
result = String.IsNullOrEmpty(emptyString); // true
result = String.IsNullOrEmpty(whitespaceString); // false
result = String.IsNullOrEmpty(nonEmptyString); // false
result = String.IsNullOrWhiteSpace(nullString); // true
result = String.IsNullOrWhiteSpace(emptyString); // true
result = String.IsNullOrWhiteSpace(whitespaceString); // true
result = String.IsNullOrWhiteSpace(nonEmptyString); // false
Run Code Online (Sandbox Code Playgroud)
Moh*_*yan 153
实践中的差异:
string testString = "";
Console.WriteLine(string.Format("IsNullOrEmpty : {0}", string.IsNullOrEmpty(testString)));
Console.WriteLine(string.Format("IsNullOrWhiteSpace : {0}", string.IsNullOrWhiteSpace(testString)));
Console.ReadKey();
Result :
IsNullOrEmpty : True
IsNullOrWhiteSpace : True
**************************************************************
string testString = " MDS ";
IsNullOrEmpty : False
IsNullOrWhiteSpace : False
**************************************************************
string testString = " ";
IsNullOrEmpty : False
IsNullOrWhiteSpace : True
**************************************************************
string testString = string.Empty;
IsNullOrEmpty : True
IsNullOrWhiteSpace : True
**************************************************************
string testString = null;
IsNullOrEmpty : True
IsNullOrWhiteSpace : True
Run Code Online (Sandbox Code Playgroud)
Iva*_*lov 37
它们是不同的功能.你应该根据自己的情况决定你需要什么.
我不认为使用它们中的任何一种都是不好的做法.大部分时间IsNullOrEmpty()
都足够了.但你有选择:)
dek*_*dev 28
这是两种方法的实际实现(使用dotPeek反编译)
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public static bool IsNullOrEmpty(string value)
{
if (value != null)
return value.Length == 0;
else
return true;
}
/// <summary>
/// Indicates whether a specified string is null, empty, or consists only of white-space characters.
/// </summary>
///
/// <returns>
/// true if the <paramref name="value"/> parameter is null or <see cref="F:System.String.Empty"/>, or if <paramref name="value"/> consists exclusively of white-space characters.
/// </returns>
/// <param name="value">The string to test.</param>
public static bool IsNullOrWhiteSpace(string value)
{
if (value == null)
return true;
for (int index = 0; index < value.Length; ++index)
{
if (!char.IsWhiteSpace(value[index]))
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
小智 6
它说这一切IsNullOrEmpty()
都不包括白色间距IsNullOrWhiteSpace()
!
IsNullOrEmpty()
如果字符串是:
-null
-empty
IsNullOrWhiteSpace()
如果字符串是:
-null
-empty
-包含白色空间只有