String.IsNullOrWhiteSpace(String) Méthode
Définition
Indique si une chaîne spécifiée est null
, vide ou se compose uniquement d'espaces blancs.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);
public static bool IsNullOrWhiteSpace (string? value);
static member IsNullOrWhiteSpace : string -> bool
Public Shared Function IsNullOrWhiteSpace (value As String) As Boolean
Paramètres
- value
- String
Chaîne à tester.The string to test.
Retours
true
si le paramètre value
est null
ou Empty, ou si value
est composé exclusivement d'espaces blancs.true
if the value
parameter is null
or Empty, or if value
consists exclusively of white-space characters.
Exemples
L’exemple suivant crée un tableau de chaînes, puis passe chaque élément du tableau à la IsNullOrWhiteSpace méthode.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
Remarques
IsNullOrWhiteSpace est une méthode pratique qui est similaire au code suivant, sauf qu’elle offre des performances supérieures :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
Les espaces blancs sont définis par la norme Unicode.White-space characters are defined by the Unicode standard. La IsNullOrWhiteSpace méthode interprète tout caractère qui retourne une valeur true
lorsqu’elle est passée à la Char.IsWhiteSpace méthode sous la forme d’un espace blanc.The 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.