String.IsNullOrWhiteSpace Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Indicates whether a specified string is nullNothingnullptra null reference (Nothing in Visual Basic), empty, or consists only of white-space characters.

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

Syntax

Public Shared Function IsNullOrWhiteSpace ( _
    value As String _
) As Boolean
public static bool IsNullOrWhiteSpace(
    string value
)

Parameters

Return Value

Type: System..::.Boolean
true if the value parameter is nullNothingnullptra null reference (Nothing in Visual Basic) or String..::.Empty, or if value consists exclusively of white-space characters.

Remarks

IsNullOrWhiteSpace is a convenience method that is equivalent to the following code, except that it offers superior performance:

Return String.IsNullOrEmpty(value) OrElse value.Trim().Length = 0
return String.IsNullOrEmpty(value) || 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.

Examples

The following example creates a string array, and then passes each element of the array to the IsNullOrWhiteSpace method.

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      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
         outputBlock.Text &= String.IsNullOrWhiteSpace(value) & vbCrLf
      Next
   End Sub
End Module
' The example displays the following output:
'       True
'       True
'       False
'       True
'       True
'       True
using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string[] values = { null, String.Empty, "ABCDE", 
                          new String(' ', 20), "  \t   ", 
                          new String('\u2000', 10) };
      foreach (string value in values)
         outputBlock.Text += String.IsNullOrWhiteSpace(value) + "\n";
   }
}
// The example displays the following output:
//       True
//       True
//       False
//       True
//       True
//       True

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1

Platforms

Windows Phone

See Also

Reference

String Class

System Namespace

IsNullOrEmpty