Best way to check if a string is empty
Hi all,
I am just wondering how you all check if a string is empty in C#.
I am a C/C++ programmer, and I usually check if the string is empty with the following code.
Code:
if( strString.length() == 0 || strString.compare( "" ) == 0 )
// string is empty
else
// do something useful with the string
so naturally, I am checking if a string is empty with the following code in c#
Code:
if( strString.length == 0 || strString.CompareTo( "" ) == 0 )
// string is empty
Is there a better way to check if a string is empty in C#?
Thank you,
kab
Re: Best way to check if a string is empty
Code:
if(String.IsNullOrEmpty(str))
{
//...
}
Re: Best way to check if a string is empty
Assuming you do not need to differentiate between null and empty :D:D
If you want to check specifically for empty...
Code:
if (stringVar != String.Empty)
{
// It is NOT empty...however it might be Null.....
}
Re: Best way to check if a string is empty
Ahh, I guess both functions are helpful, but I think IsNullOrEmpty is better for me =)
Thanks to both.
Hmm, TheCPUWizard, I can't give you another reputation. It seems like I have to wait for longer..
Re: Best way to check if a string is empty
Quote:
Originally Posted by
kabilius
Code:
if( strString.length() == 0 || strString.compare( "" ) == 0 )
In what language would an empty string *not* compare with "" to give the value of '0'. Isn't that a redundant statement no matter what language you're in?
Re: Best way to check if a string is empty
Quote:
Originally Posted by
Mutant_Fruit
In what language would an empty string *not* compare with "" to give the value of '0'. Isn't that a redundant statement no matter what language you're in?
Hi Mutant_Fruit,
Are you saying strString.length() == 0 is equivalent to strString.compare( "" ) == 0?
Yeah, now that I take a closer look, I think they are equivalent....
I don't remember why I put these two statements together, maybe it was just a way to make me feel that I am performing plentful sanity checks.....
kab
Re: Best way to check if a string is empty
Quote:
Originally Posted by
kabilius
maybe it was just a way to make me feel that I am performing plentful sanity checks.....
Sanity checks are great, but only if they add value. In this case, it's redundant.
Re: Best way to check if a string is empty
Quote:
Originally Posted by
Arjay
Sanity checks are great, but only if they add value. In this case, it's redundant.
Yes sir, I understand. I was trying to find a way out to explain why I have this redundant statement, and making me feel safer about my code was the best reason I found.... =p
Thanks for the input, I will try not to write statements like this in the future
kab
Re: Best way to check if a string is empty
I do it like this, I added the Trim method just to eliminate spaces.
Code:
if (strString.Trim().Length == 0)
Re: Best way to check if a string is empty
that's just one of those things that would get noticed and discussed during a code review. and why code reviews are such a worth while endeavor.
Re: Best way to check if a string is empty
Quote:
Originally Posted by
dee-u
I do it like this, I added the Trim method just to eliminate spaces.
Code:
if (strString.Trim().Length == 0)
I would not recommend doing that in any code that someone else has to modify. For one, it is inefficient (you are calling two methods instead of comparing against a static value), two, it is not clear why you would do that, and three, it is incorrect. however unlikely it may be, a string containing only spaces is not an empty string. You are not checking the value of the original string, you are creating a new string and checking its value.
Re: Best way to check if a string is empty
Quote:
Originally Posted by
BigEd781
I would not recommend doing that in any code that someone else has to modify. For one, it is inefficient (you are calling two methods instead of comparing against a static value), two, it is not clear why you would do that, and three, it is incorrect. however unlikely it may be, a string containing only spaces is not an empty string. You are not checking the value of the original string, you are creating a new string and checking its value.
Agreed. I try to avoid situations where extra leading or trailing spaces are added or when they are added, they're a necessary part of the string.
If I don't need the spaces, I'll trim them before I store them into the string.
That way any subsequent checks are reduced to
Code:
if( !String.IsNullOrEmpty( str ) )
{
//...
}
Re: Best way to check if a string is empty
Quote:
Originally Posted by
BigEd781
I would not recommend doing that in any code that someone else has to modify. For one, it is inefficient (you are calling two methods instead of comparing against a static value), two, it is not clear why you would do that, and three, it is incorrect. however unlikely it may be, a string containing only spaces is not an empty string. You are not checking the value of the original string, you are creating a new string and checking its value.
Not only that, it will CRASH on a null string. :eek::eek:
TOTALLY DEFEATING the original purpose!
Re: Best way to check if a string is empty
I'm glad to learn something which I was not aware of. Thanks guys!
Edit:
Actually, the primary reason I am using that is I am using this VB6.0 code to determine the 'emptiness' of a string and this is the most officient method I have known so far.
Code:
If Len(Trim$(x)) = 0 Then
All I thought it would convert to the .Net code I posted earlier.
Re: Best way to check if a string is empty
Sorry for hijacking this thread but I just need a little more clarification.
For instance I have a Textbox that is used to input a Last Name that goes to a specific field in a table. A user pressed the Space key in the keyboard for that Textbox. If I need to store in a string the value of that textbox then how should I proceed?