Weyland Yutani
Weyland Yutani

Reputation: 4960

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

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))
    {

    }
}

Upvotes: 0

Views: 161

Answers (5)

Simon Bull
Simon Bull

Reputation: 881

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

Upvotes: 0

Matthias Müller
Matthias Müller

Reputation: 3483

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?

Upvotes: 0

Rik
Rik

Reputation: 29243

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 == "") 
{
    ...
}

Upvotes: 0

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48600

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. "".

Upvotes: 1

coolerfarmer
coolerfarmer

Reputation: 385

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

Always use this!

Upvotes: 2

Related Questions