Wednesday 20 June 2007

Is empty String ?

How to test that a String is empty?

A lot of people use:


myString.equals("")

Some use:

"".equals(myString)

Few use:

myString.length() == 0

Fewer use:

myString != null && myString.length() == 0

Lazy people use:

org.apache.commons.lang.StringUtils.isEmpty(myString)

Which one is the best solution ?

I tell you ! Be lazy ! Knows libraries like you learn new word from dictionary.
Why ? Because, apache people ARE good. Because it is already done by another. Because it is free.

Else? Use

myString != null && myString.length() == 0

because a String might be null and because it is really fast to test the String length instead of its content.

No comments: