String.IsNullOrWhiteSpace(String) 方法

定義

表示指定的字串是否為 null、空白,或只由空白字元組成的字串。

public:
 static bool IsNullOrWhiteSpace(System::String ^ value);
public static bool IsNullOrWhiteSpace (string value);
public static bool IsNullOrWhiteSpace (string? value);
static member IsNullOrWhiteSpace : string -> bool
Public Shared Function IsNullOrWhiteSpace (value As String) As Boolean

參數

value
String

要測試的字串。

傳回

如果 true 參數是 valuenull,或者 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 會將傳回 值 true 的任何字元解譯為空白字元時傳回 Char.IsWhiteSpace 的值。

適用於

另請參閱