|
-
December 26th, 2008, 05:47 PM
#1
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
-
December 26th, 2008, 05:49 PM
#2
Re: Best way to check if a string is empty
Code:
if(String.IsNullOrEmpty(str))
{
//...
}
-
December 26th, 2008, 06:01 PM
#3
Re: Best way to check if a string is empty
Assuming you do not need to differentiate between null and empty  
If you want to check specifically for empty...
Code:
if (stringVar != String.Empty)
{
// It is NOT empty...however it might be Null.....
}
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
December 26th, 2008, 06:15 PM
#4
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..
Last edited by kabilius; December 26th, 2008 at 06:26 PM.
-
December 26th, 2008, 08:16 PM
#5
Re: Best way to check if a string is empty
 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?
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
December 29th, 2008, 02:29 PM
#6
Re: Best way to check if a string is empty
 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
-
December 29th, 2008, 02:34 PM
#7
Re: Best way to check if a string is empty
 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.
-
December 29th, 2008, 04:12 PM
#8
Re: Best way to check if a string is empty
 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
-
December 29th, 2008, 04:23 PM
#9
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)
-
December 29th, 2008, 04:35 PM
#10
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.
-
December 29th, 2008, 04:58 PM
#11
Re: Best way to check if a string is empty
 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.
-
December 29th, 2008, 05:26 PM
#12
Re: Best way to check if a string is empty
 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 ) )
{
//...
}
-
December 29th, 2008, 05:37 PM
#13
Re: Best way to check if a string is empty
 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.  
TOTALLY DEFEATING the original purpose!
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
December 29th, 2008, 06:11 PM
#14
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.
Last edited by dee-u; December 29th, 2008 at 06:15 PM.
-
December 29th, 2008, 06:26 PM
#15
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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|