String.IsNullOrWhiteSpace(String) Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Anger om en angiven sträng är null, tom eller endast består av blankstegstecken.
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
Parametrar
- value
- String
Strängen som ska testas.
Returer
true om parametern value är null eller Empty, eller om value endast består av blankstegstecken.
Exempel
I följande exempel skapas en strängmatris och varje element i matrisen skickas IsNullOrWhiteSpace sedan till metoden.
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
Kommentarer
IsNullOrWhiteSpace är en bekvämlighetsmetod som liknar följande kod, förutom att den ger överlägsen prestanda:
return String.IsNullOrEmpty(value) || value.Trim().Length == 0;
String.IsNullOrEmpty value || value.Trim().Length = 0
Return String.IsNullOrEmpty(value) OrElse value.Trim().Length = 0
Blankstegstecken definieras av Unicode-standarden. Metoden IsNullOrWhiteSpace tolkar alla tecken som returnerar ett värde för true när det skickas till Char.IsWhiteSpace metoden som ett blankstegstecken.