Condividi tramite


String.IsNullOrWhiteSpace Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
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 nulla 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

Silverlight

Supported in: 5, 4

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.