String.IsNullOrWhiteSpace(String) Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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
- value
- String
The string to test.
true
if the value
parameter is null
or Empty, or if value
consists exclusively of white-space characters.
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
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
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;
String.IsNullOrEmpty value || value.Trim().Length = 0
Return String.IsNullOrEmpty(value) OrElse value.Trim().Length = 0
White-space characters are defined by the Unicode standard. 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.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: