String.IsNullOrWhiteSpace(String) 方法    
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指示指定的字符串是 null、空还是仅由空白字符组成。
public:
 static bool IsNullOrWhiteSpace(System::String ^ value);public static bool IsNullOrWhiteSpace(string value);public static bool IsNullOrWhiteSpace(string? value);static member IsNullOrWhiteSpace : string -> boolPublic Shared Function IsNullOrWhiteSpace (value As String) As Boolean参数
- value
- String
要测试的字符串。
返回
如果 true 参数为 value 或 null,或者如果 Empty 仅由空白字符组成,则为 value。
示例
以下示例创建一个字符串数组,然后将数组的每个元素传递给 IsNullOrWhiteSpace 方法。
using System;
public class Example
{
   public static void Main()
   {
      string[] values = { null, String.Empty, "ABCDE", 
                          new String(' ', 20), "  \t   ", 
                          new String('\u2000', 10) };
      foreach (string value in values)
         Console.WriteLine(String.IsNullOrWhiteSpace(value));
   }
}
// The example displays the following output:
//       True
//       True
//       False
//       True
//       True
//       True
open System
let values = 
    [| null; String.Empty; "ABCDE"
       String(' ', 20); "  \t   "
       String('\u2000', 10) |]
for value in values do
    printfn $"{String.IsNullOrWhiteSpace value}"
// The example displays the following output:
//       True
//       True
//       False
//       True
//       True
//       True
Module Example
   Public Sub Main()
      Dim values() As String = { Nothing, String.Empty, "ABCDE", 
                                 New String(" "c, 20), "  " + vbTab + "   ", 
                                 New String(ChrW(&h2000), 10) }
      For Each value As String In values
         Console.WriteLine(String.IsNullOrWhiteSpace(value))
      Next
   End Sub
End Module
' The example displays the following output:
'       True
'       True
'       False
'       True
'       True
'       True
注解
IsNullOrWhiteSpace 是一种类似于以下代码的便捷方法,只不过它提供卓越的性能:
return String.IsNullOrEmpty(value) || value.Trim().Length == 0;
String.IsNullOrEmpty value || value.Trim().Length = 0
Return String.IsNullOrEmpty(value) OrElse value.Trim().Length = 0
空格字符由 Unicode 标准定义。 方法IsNullOrWhiteSpace将返回 值的任何字符解释为空白字符传递给Char.IsWhiteSpace方法时返回的值true。