String.IsNullOrWhiteSpace(String) Metoda

Definicja

Wskazuje, czy określony ciąg to null, pusty, czy składa się tylko z białych znaków.

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

Parametry

value
String

Ciąg do przetestowania.

Zwraca

truevalue jeśli parametr ma null wartość lub Empty, lub value składa się wyłącznie z znaków odstępu.

Przykłady

Poniższy przykład tworzy tablicę ciągów, a następnie przekazuje każdy element tablicy do IsNullOrWhiteSpace metody .

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

Uwagi

IsNullOrWhiteSpace jest wygodną metodą podobną do następującego kodu, z tą różnicą, że oferuje lepszą wydajność:

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

Znaki odstępu są definiowane przez standard Unicode. Metoda IsNullOrWhiteSpace interpretuje dowolny znak, który zwraca wartość true , gdy jest przekazywany do Char.IsWhiteSpace metody jako znak odstępu.

Dotyczy

Zobacz też