String.IsNullOrWhiteSpace Method
Indicates whether a specified string is Nothing, empty, or consists only of white-space characters.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- value
- Type: System.String
The string to test.
Return Value
Type: System.Booleantrue if the value parameter is Nothing 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:
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
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
The method will test if the input string is Nothing (null in C#) before trying to see if it contains only whitespace.
This highlights the following possible naming confusion:
VB: Nothing
- empty reference for object types or default value when used in an assignment for simple types
- empty reference for object types or default value when used in an assignment for simple types
- the .NET equivalent of the SQL NULL
The same criticism applies to IsNullOrEmpty.
- 6/16/2011
- James Close
