How can I check if a string is empty?

  • Thread starter Thread starter Hamed
  • Start date Start date
I mean if the string is equal to "" or has blank characters such as tab or
space, it all cases supposed are empty
 
Hamed,

You can check it against the Empty constant on the string class, which
is an empty string. This is different from a null reference, which means
that you don't refer to any string at all. For the most part, they are
probably the same, but if you want to check against all cases, you can do
the following:

bool pblnEmpty = (str == null || str.Length == 0);

Hope this helps.
 
looking for empty or containing a space... this will work.
Also, if the list of characters you want to consider a "space" character is
outside the normal set, you can specify them in the String.Trim method that
I use in the second "if" statement below.

Hope this helps,
---- Nick

public bool IsMyStringEmpty(string Target)
{
if (Target.Length == 0)
return true;
else if (Target.Trim().Length == 0)
return true;
return false;
}
 
Back
Top