What is the best way to check if a string is empty in C# in VS2005?
There's the builtin String.IsNullOrEmpty
which I'd use. It's described here.
try this one:
if (string.IsNullOrEmpty(YourStringVariable))
{
//TO Do
}
As suggested above you can use String.IsNullOrEmpty, but that will not work if you also want to check for strings with only spaces (some users place a space when a field is required). In that case you can use:
if(String.IsNullOrEmpty(str) || str.Trim().Length == 0) {
// String was empty or whitespaced
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With