String.IsNullOrWhiteSpace(String) Methode

Definition

Gibt an, ob eine angegebene Zeichenfolge leer ist nulloder nur aus Leerzeichen besteht.

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

Parameter

value
String

Die zu testende Zeichenfolge.

Gibt zurück

true wenn der value Parameter null oder Empty, oder wenn value ausschließlich aus Leerzeichen besteht.

Beispiele

Im folgenden Beispiel wird ein Zeichenfolgenarray erstellt und dann jedes Element des Arrays an die IsNullOrWhiteSpace Methode übergeben.

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

Hinweise

IsNullOrWhiteSpace ist eine Komfortmethode, die dem folgenden Code ähnelt, mit der Ausnahme, dass sie eine überlegene Leistung bietet:

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

Leerzeichen werden durch den Unicode-Standard definiert. Die IsNullOrWhiteSpace Methode interpretiert jedes Zeichen, das true einen Wert zurückgibt, wenn es als Leerzeichen an die Char.IsWhiteSpace Methode übergeben wird.

Gilt für:

Weitere Informationen