CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 43
  1. #1
    Join Date
    Jun 2007
    Location
    California
    Posts
    136

    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

  2. #2
    Join Date
    Feb 2007
    Location
    Craiova, Romania
    Posts
    326

    Re: Best way to check if a string is empty

    Code:
    if(String.IsNullOrEmpty(str)) 
    {
    //...
    }

  3. #3
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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

  4. #4
    Join Date
    Jun 2007
    Location
    California
    Posts
    136

    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.

  5. #5
    Join Date
    May 2007
    Posts
    1,546

    Re: Best way to check if a string is empty

    Quote Originally Posted by kabilius View Post
    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.

  6. #6
    Join Date
    Jun 2007
    Location
    California
    Posts
    136

    Re: Best way to check if a string is empty

    Quote Originally Posted by Mutant_Fruit View Post
    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

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Best way to check if a string is empty

    Quote Originally Posted by kabilius View Post
    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.

  8. #8
    Join Date
    Jun 2007
    Location
    California
    Posts
    136

    Re: Best way to check if a string is empty

    Quote Originally Posted by Arjay View Post
    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

  9. #9
    Join Date
    Jan 2006
    Location
    Pearl of the orient
    Posts
    304

    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)

  10. #10
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    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.

  11. #11
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Best way to check if a string is empty

    Quote Originally Posted by dee-u View Post
    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.

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Best way to check if a string is empty

    Quote Originally Posted by BigEd781 View Post
    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 ) ) 
    {
      //...
    }

  13. #13
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Best way to check if a string is empty

    Quote Originally Posted by BigEd781 View Post
    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

  14. #14
    Join Date
    Jan 2006
    Location
    Pearl of the orient
    Posts
    304

    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.

  15. #15
    Join Date
    Jan 2006
    Location
    Pearl of the orient
    Posts
    304

    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?

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured