String.IsNullOrWhiteSpace(String) Metodo

Definizione

Indica se una stringa specificata è null, vuota o è costituita solo da spazi vuoti.

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

Parametri

value
String

Stringa da testare.

Valori restituiti

true se il value parametro è null o Emptyo se value è costituito esclusivamente da spazi vuoti.

Esempio

Nell'esempio seguente viene creata una matrice di stringhe e quindi viene passato ogni elemento della matrice al IsNullOrWhiteSpace metodo .

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

Commenti

IsNullOrWhiteSpace è un metodo pratico simile al codice seguente, ad eccezione del fatto che offre prestazioni superiori:

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

Gli spazi vuoti sono definiti dallo standard Unicode. Il IsNullOrWhiteSpace metodo interpreta qualsiasi carattere che restituisce un valore di true quando viene passato al Char.IsWhiteSpace metodo come carattere di spazio vuoto.

Si applica a

Vedi anche