String.IsNullOrWhiteSpace(String) String.IsNullOrWhiteSpace(String) String.IsNullOrWhiteSpace(String) String.IsNullOrWhiteSpace(String) Method

定義

表示指定的字串是否為 null、空白,或只由空白字元組成的字串。Indicates whether a specified string is null, empty, or consists only of white-space characters.

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

參數

value
String String String String

要測試的字串。The string to test.

傳回

如果 true 參數是 valuenull,或者 Empty 完全由空白字元組成,則為 valuetrue if the value parameter is null or Empty, or if value consists exclusively of white-space characters.

範例

下列範例會建立字串陣列, 然後將陣列的每個元素傳遞至IsNullOrWhiteSpace方法。The following example creates a string array, and then passes each element of the array to the IsNullOrWhiteSpace method.

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
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是與下列程式碼類似的便利方法, 不同之處在于它會提供較佳的效能:IsNullOrWhiteSpace is a convenience method that is similar to the following code, except that it offers superior performance:

return String.IsNullOrEmpty(value) || value.Trim().Length == 0;
Return String.IsNullOrEmpty(value) OrElse value.Trim().Length = 0

空白字元是由 Unicode 標準所定義。White-space characters are defined by the Unicode standard. 方法會在傳遞至Char.IsWhiteSpace方法做為空白字元時true , 解讀傳回值的任何字元。 IsNullOrWhiteSpaceThe IsNullOrWhiteSpace method interprets any character that returns a value of true when it is passed to the Char.IsWhiteSpace method as a white-space character.

適用於

另請參閱