0

I usually do this:

void Incoming(String str)
{
    if (str == "")
    {

    }
}

But I've seen others' code do this:

void Incoming(String str)
{
    if (str == String.Empty)
    {

    }
}

In my case I know str shouldn't be null. So would the following be a good idea instead, given it will cause an exception if the string is null rather than hiding the problem:

void Incoming(String str)
{
    if (str.Length == 0)
    {

    }
}

And I've noticed this being used too, but this seems to accept the string might be null, rather than going and fixing the reason it is null. Is it a good idea to silently handle strings being null?

void Incoming(String str)
{
    if (String.IsNullOrEmpty(str))
    {

    }
}
Weyland Yutani
  • 3,904
  • 1
  • 18
  • 25
  • 1
    @Mehrdad: what if str is `null`. In that case exception will be thrown. – Nikhil Agrawal Jul 09 '14 at 10:35
  • http://stackoverflow.com/questions/263191/in-c-should-i-use-string-empty-or-string-empty-or – Soner Gönül Jul 09 '14 at 10:35
  • @Mehrdad If str is null that will throw. Can't trust function arguments and whatnot. – Slate Jul 09 '14 at 10:35
  • @kjhf: [Read the OP's comment below first](http://stackoverflow.com/questions/24651291/what-is-the-best-way-of-testing-that-a-string-is-empty?noredirect=1#comment38212305_24651319) – user541686 Jul 09 '14 at 10:36
  • i don't think i worded the question title well. or the question. – Weyland Yutani Jul 09 '14 at 10:38
  • *"Is it a good idea to silently handle strings being null?"* sometimes yes, sometimes no. If you're going to assume it is non-null later in the function, then no -- make it crash. If it doesn't matter to the rest of the function, then yes; there's no need to make it crash in that case, just make it behave the way the caller would expect. – user541686 Jul 09 '14 at 10:42
  • Suppose we already know that `null` is not an option. So I think the Original Poster wants to know if `void Incoming(string str) { if (str == null) { throw new ArgumentNullException("str"); } if (str == "") { /* ... */ } /* ... */ }` is any worse than `void Incoming(string str) { if (str == null) { throw new ArgumentNullException("str"); } if (str.Length == 0) { /* ... */ } /* ... */ }`. That is not addressed by the threads currently chosen as duplicates. – Jeppe Stig Nielsen Jul 09 '14 at 10:53
  • If the string *should never* be null, do an explicit null check before testing whether it's empty. – Rik Jul 09 '14 at 11:00
  • @Rik Good advice. Then right after that null check, what should his check for emptiness then look like? Either `str == ""` or `str.Length == 0`? – Jeppe Stig Nielsen Jul 09 '14 at 11:01
  • I'd say `str == ""` because its intent is clearer, and it's shorter, too. – Rik Jul 09 '14 at 11:08

5 Answers5

2
if(String.IsNullOrEmpty(str1))
{
     //do stuff
}

Always use this!

Mike G
  • 4,022
  • 9
  • 41
  • 63
coolerfarmer
  • 385
  • 2
  • 9
  • even if str1 shouldn't be null? – Weyland Yutani Jul 09 '14 at 10:33
  • @kjhf: Clearly the OP said "shouldn't". – user541686 Jul 09 '14 at 10:37
  • @kjhf: In fact, if it was "isn't", then you should use `IsNullOrEmpty`. But in the case of "shouldn't" then you should make the program crash on the null case instead of continuing on (since an assertion of your logic failed), which is why I posted my comment regarding `.Length` earlier. – user541686 Jul 09 '14 at 10:38
  • @Mehrdad Then you need to null check it. Your point above was that you don't need to null check it, but OP said shouldn't not isn't. Therefore a null check using String.IsNullOrEmpty is appropriate. – Slate Jul 09 '14 at 10:38
1

What is the best way of testing that a string is empty?

Your last option String.IsNullOrEmpty(str) is best to do so. It checks for both condition

  1. If string is null
  2. If your string is equal to String.Empty i.e. "".
Nikhil Agrawal
  • 42,396
  • 22
  • 107
  • 187
0

Generally you should not use Exceptions for System flow. So if the String can (but shouldn't) be null, then check for NullOrEmpty and fix it there. Whats the matter for checking if it is NULL or Empty? Both will cause some fixing Code or you can throw your own "IHateNullStrings"-Exception.

If you want the total safety:

            String.IsNullOrWhiteSpace

With this one you check

  • Null
  • Empty
  • Only WhiteSpace characters

Edit: Exception Flow is the path the program passes. Using exception for "Could happen"-things makes f.e. debugging way more complicated. There is a huge discussion about this, f.e. :Why not use exceptions as regular flow of control?

Community
  • 1
  • 1
Matthias Müller
  • 3,064
  • 2
  • 28
  • 55
0

If the string should never be null, do an explicit null check before testing whether it's empty.

if (string == null) throw new Exception(); // ArgumentNullException, for example
if (string == "") 
{
    ...
}
Rik
  • 26,673
  • 13
  • 47
  • 65
0

Although you say str should not be null, you are relying on something external to adhere to that, which is not a good idea. Also, letting an exception be thrown in the if block is not a great idea because:

  1. Exceptions are expense, so should be avoided where possible
  2. The default exception is not going to give the caller much context as to what they have done wrong. So if you really need an exception, throw one yourself with something meaningful (I.E. ArgumentNullException) with a useful message

so with the above said you can either do a seperate null check:

if (str == null) throw new ArgumentNullException("str cannot be null");

or you can treat null and string.Empty as equal and use (which would be my default):

if (string.IsNullOrEmpty(str))
{
    //do something
}

or you can even treat blank strings the same as null and empty by using:

if (string.IsNullOrWhiteSpace(str))
{
    /do something
}

It largely depends on what your logic is when you get empty strings. However you should always be writing some defensive code to deal with arguments not fulfilling the pre-conditions

Simon Bull
  • 841
  • 6
  • 13