String.IsNullOrWhiteSpace(String) Método
Definición
Indica si la cadena especificada es null
, está vacía o consta únicamente de caracteres de espacio en blanco.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
Parámetros
- value
- String
Cadena que se va a comprobar.The string to test.
Devoluciones
Es true
si el parámetro value
es null
o Empty, o bien si value
consta únicamente de caracteres de espacio en blanco.true
if the value
parameter is null
or Empty, or if value
consists exclusively of white-space characters.
Ejemplos
En el ejemplo siguiente se crea una matriz de cadenas y, a continuación, se pasa cada elemento de la matriz al IsNullOrWhiteSpace método.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
Comentarios
IsNullOrWhiteSpace es un método útil similar al código siguiente, con la salvedad de que ofrece un rendimiento superior: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
El estándar Unicode define los caracteres de espacio en blanco.White-space characters are defined by the Unicode standard. El IsNullOrWhiteSpace método interpreta cualquier carácter que devuelva un valor de true
cuando se pasa al Char.IsWhiteSpace método como un carácter de espacio en blanco.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.