This topic has not yet been rated - Rate this topic

String.IsNullOrWhiteSpace Method

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

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
)

Parameters

value
Type: System.String
The string to test.

Return Value

Type: System.Boolean
true if the value parameter is null or String.Empty, 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;


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.

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


.NET Framework

Supported in: 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

Windows 8 Release Preview, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 SP2, Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)