StringIsNullOrWhiteSpace Method
 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Indicates whether a specified string is null, empty, or consists only of white-space characters.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public static bool IsNullOrWhiteSpace(
	string value
)
public:
static bool IsNullOrWhiteSpace(
	String^ value
)
static member IsNullOrWhiteSpace : 
        value:string -> bool
Public Shared Function IsNullOrWhiteSpace (
	value As String
) As Boolean

Parameters

value
Type:

The string to test.

Return Value

Type:

true if the value parameter is null or StringEmpty, or if value consists exclusively of white-space characters.

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

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 CharIsWhiteSpace method as a white-space character.

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
Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 4.0
Windows Phone Silverlight
Available since 7.1
Windows Phone
Available since 8.1
Return to top