- if (ourString == null)
- if (ourString == "")
- if (ourString.trim() == "")
- if (ourString == string.empty)
Luckily, Microsoft have came up with this great method "IsNullOrEmpty" which has something like this:
return val == null || val == String.Empty;
This is a great function thought it doesn't cover 100% of the cases. Lets say, for example, that I have this string: " ".
Now, according to Microsoft the IsNullOrEmpty method will return false.
Unfortunately, this is not a good result for us.
In order to fix this in our projects, we added this small method:
public static bool IsNullOrEmpty(this String val)
return val == null || val.trim() == String.Empty;
This will resolve our problem.
So now we can use:
bool b = String.IsNullOrEmpty(" ");And we will get true.
.Net 4 Users:
For you guys Microsoft fixed that thing in the form of
bool b = string.IsNullOrWhiteSpace(" ");Which will return true.
CTO at ITweetLive.com