Home
Manage Your Code
Snippet: Tests whether a string is null, empty or blank (C#)
Title: Tests whether a string is null, empty or blank Language: C#
Description: Tests whether a string is null, empty or blank (i.e., contains only spaces). It's an enhancement of .NET 2.0's String.IsNullOrEmpty method. Views: 506
Author: Kevin McFarlane Date Added: 12/12/2006
Copy Code  
1/// <summary>

2/// Is the specified String object a null reference, 

3/// an Empty string or a string containing only spaces?

4/// </summary>

5/// <param name="arg">A String reference.</param>

6/// <returns>

7/// 	<c>true</c> if the string is null or empty or blank; otherwise, <c>false</c>.

8/// </returns>

9public static bool IsNullOrEmptyOrBlank(string arg)
10{
11    return String.IsNullOrEmpty(arg) || arg.Trim().Length == 0;
12}
13
Usage
string s = null;

if (IsNullOrEmptyOrBlank(s))
{
    Console.WriteLine("True");
}
else
{
    Console.WriteLine("False");
}