0

In my app, I want to check if a string variable is empty.

I've handled it as follows,

 if ((Name == null) || (Name == ""))
  {
     //Handled
  }

But it passes this condition, if the value is given as " "(whitespace). How can i detect if the variable contains only whitespaces??

Thanks in advance!

Naren
  • 2,231
  • 1
  • 25
  • 45
user3008134
  • 127
  • 2
  • 11

4 Answers4

4

Use String.IsNullOrWhiteSpace:

if (String.IsNullOrWhiteSpace(Name))
{
    //Handled
}
Chris Mantle
  • 6,595
  • 3
  • 34
  • 48
3
String.IsNullOrWhiteSpace(Name)

And MSDN article about it

Anton Sizikov
  • 9,105
  • 1
  • 28
  • 39
2

Use String.IsNullOrEmpty to check null or empty and String.IsNullOrWhiteSpace to check null or whitespace

Chris Mantle
  • 6,595
  • 3
  • 34
  • 48
A.K.
  • 3,321
  • 1
  • 15
  • 27
1

Use String.IsNullOrEmpty to check if it is null and String.IsNullOrWhiteSpace to chack for whitespace!! For your case String.IsNullOrWhitespace(Name)

Anton Sizikov
  • 9,105
  • 1
  • 28
  • 39
Aju
  • 796
  • 3
  • 10
  • 29