.NET 4.0 New Feature - String.IsNullOrWhiteSpace
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string[] values = { null, String.Empty, “ABCDE”, new String(’ ‘, 20), " “, new String(’\u2000’, 10), };
Console.WriteLine(new String('=', 50));
Console.WriteLine("IsNullOrWhiteSpaceDemo...");
IsNullOrWhiteSpaceDemo(values);
Console.WriteLine(new String('=', 50));
Console.WriteLine("MSDNCustomIsNullOrWhiteSpaceDemo...");
MSDNCustomIsNullOrWhiteSpaceDemo(values);
Console.WriteLine(new String('=', 50));
Console.WriteLine("CustomIsNullOrWhiteSpaceDemo...");
CustomIsNullOrWhiteSpaceDemo(values);
}
public static void IsNullOrWhiteSpaceDemo(string[] values)
{
foreach (string value in values)
Console.WriteLine("String.IsNullOrWhiteSpace({0}): {1}", value, String.IsNullOrWhiteSpace(value));
}
public static void MSDNCustomIsNullOrWhiteSpaceDemo(string[] values)
{
foreach (string value in values)
Console.WriteLine("MSDNCustomIsNullOrWhiteSpace({0}): {1}", value, MSDNCustomIsNullOrWhiteSpace(value));
}
public static bool MSDNCustomIsNullOrWhiteSpace(string value)
{
return String.IsNullOrEmpty(value) || value.Trim().Length == 0;
}
public static void CustomIsNullOrWhiteSpaceDemo(string[] values)
{
foreach (string value in values)
Console.WriteLine("CustomIsNullOrWhiteSpace({0}): {1}", value, CustomIsNullOrWhiteSpace(value));
}
public static bool CustomIsNullOrWhiteSpace(string value)
{
return value == null || value.Trim().Length == 0;
}
}
}