string.IsNullOrEmpty(string)与string.IsNullOrWhiteSpace(string)

当在.NET 4.0及以上版本中存在string.IsNullOrEmpty(string)时,检查被认为是不正确练习的string时,是否使用string.IsNullOrWhiteSpace(string)

最好的做法是select最合适的一个。

.Net Framework 4.0 Beta 2为string提供了一个新的IsNullOrWhiteSpace()方法,该方法将IsNullOrEmpty()方法推广到除空string之外的其他空白字符。

术语“空白”包括屏幕上不可见的所有字符。 例如,空格,换行符,制表符和空string是空白字符*

参考: 这里

为了performance,IsNullOrWhiteSpace并不理想,但是很好。 方法调用会导致一个小的性能损失。 此外,如果您不使用Unicode数据,则IsWhiteSpace方法本身具有一些可以删除的间接索引。 一如既往,不成熟的优化可能是邪恶的,但也是有趣的。

参考: 这里

检查源代码 (参考源代码.NET Framework 4.6.2)

IsNullorEmpty

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

IsNullOrWhiteSpace

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

例子

 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 

实践中的差异:

 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 

它们是不同的function。 你应该为你的情况决定你需要什么。

我不认为使用其中任何一个是不好的做法。 大部分时间IsNullOrEmpty()就够了。 但你有select:)

这两个方法的实际实现(使用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; } 

它说这一切IsNullOrEmpty()不包括白色间距而IsNullOrWhiteSpace()呢!

IsNullOrEmpty()如果string是:
-空值
-empty

IsNullOrWhiteSpace()如果string是:
-空值
-empty
– 只包含白色空间

那怎么样才能抓住所有…

 if (string.IsNullOrEmpty(x.Trim()) { } 

这将修剪所有的空间,如果他们在那里避免性能损失的IsWhiteSpace,这将使string满足“空”条件,如果它不为空。

我也认为这是更清楚,它总的来说很好的做法,无论如何修剪string,特别是如果你把它们放入数据库或其他东西。

用IsNullOrEmpty和IsNullOrwhiteSpace检查一下

 string sTestes = "I like sweat peaches"; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); for (int i = 0; i < 5000000; i++) { for (int z = 0; z < 500; z++) { var x = string.IsNullOrEmpty(sTestes);// OR string.IsNullOrWhiteSpace } } stopWatch.Stop(); // Get the elapsed time as a TimeSpan value. TimeSpan ts = stopWatch.Elapsed; // Format and display the TimeSpan value. string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Console.WriteLine("RunTime " + elapsedTime); Console.ReadLine(); 

你会看到IsNullOrWhiteSpace慢得多:/