Apache's StringUtils.isBlank(str) vs. Guava's Strings.isNullOrEmpty(str): Should you routinely check for whitespace?

Bryce Marden picture Bryce Marden · Sep 7, 2011 · Viewed 22.5k times · Source

Is there any advantage in using

StringUtils.isBlank(str) 

from Apache commons-lang.

vs

Strings.isNullOrEmpty(String string)

from Google Guava?

I want to replace hundreds of cases of they following usage in a Java project:

if(str == null || str.isEmpty())

Guava's isNullOrEmpty seems to be a direct replacement for the usage above in my project.

But more people seem to use Apache's isBlank method based on my reading of S.O. questions.

The only difference seems to be that StringUtils.isBlank(str) also checks for whitespace in addition to checking whether the string is null or empty.

Normally is it a good idea to check a String for whitespace or could that produce a different result in your code than Guava's simpler check?

Answer

dcendents picture dcendents · Apr 7, 2016

If you want to use Guava to replicate the isBlank behaviour, I would use the following method instead:

Strings.nullToEmpty(str).trim().isEmpty()