What is needed to make String.IsNullOrEmpty() count whitespace strings as empty?
Eg. I want the following to return true instead of the usual false:
String.IsNullOrEmpty(" ");
Is there a better approach than:
String.IsNullOrEmpty(" ".Trim());
(Note that the original question asked what the return would be normally hence the unsympathetic comments, this has been replaced with a more sensible question).
.NET 4.0 will introduce the method
String.IsNullOrWhiteSpace. Until then you’ll need to useTrimif you want to deal with white space strings the same way you deal with empty strings.For code not using .NET 4.0, a helper method to check for
nullor empty or whitespace strings can be implemented like this:The
String.IsNullOrEmptywill not perform any trimming and will just check if the string is a null reference or an empty string.