22

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?

sebkur
  • 658
  • 2
  • 9
  • 18
Bryce Marden
  • 221
  • 1
  • 2
  • 3

5 Answers5

17

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

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

dcendents
  • 753
  • 5
  • 7
8

When you have to accept input from human beings, you should be forgiving and strip leading and trailing whitespace from whatever text they type, if it makes sense in the particular application.

That said, using isBlank is only halfbaked. You also need to trim the strings before processing them further. So I suggest to use s = trim(s); before checking with isNullOrEmpty.

Roland Illig
  • 40,703
  • 10
  • 88
  • 121
  • 2
    That’s why I wrote `s = trim(s)` and not `s = s.trim()`. `trim` would be the null-safe variant. – Roland Illig May 17 '13 at 07:39
  • But that would kind of defeat the purpose, wouldn't it? `trim(s)` would have to check for `null` before it could call `s.trim()`, at which point `Strings.isNullOrEmpty(s')` is only half as useful. `StringUtils.isBlank(s)` seems like it does all that is desired here. – Jamie Bisotti Oct 18 '13 at 16:24
  • 3
    The bad side of `isBlank` is that it doesn't fix the input, so you end up validating against the whitespace-stripped string in the front end, but you continue processing with the whitespace-including string, which sounds odd. – Roland Illig Oct 18 '13 at 19:33
  • 1
    Too bad Strings doesn't have the methods to do null-safe trimming either (e.g., StringUtils.trimToEmpty). If they did, it might show that they actually thought about usage a bit. – Hakanai Jan 28 '15 at 01:56
3

StringUtils.isBlank(str) is very much different than Strings.isNullOrEmpty(String string)

the first code sample will only check if the string is empty or not, it will include white spaces as well and return true

StringUtils.isBlank(null)      = true 
StringUtils.isBlank("")         = true 
StringUtils.isBlank(" ")        = true 
StringUtils.isBlank("bob")      = false
StringUtils.isBlank("  bob  ")  = false

where as Strings.isNullOrEmpty(String string) Returns true if the given string is null or is the empty string

0

isBlank is incredibly overrated. UI code that reads user text straight out of input fields can trim whitespace once and for all, and then you can stop worrying about it.

Kevin Bourrillion
  • 40,336
  • 12
  • 74
  • 87
  • 5
    How is `isBlank` overrated? – benson Feb 27 '13 at 23:37
  • 1
    Because UI code that reads user text straight out of input fields can trim whitespace once and for all, and then you can stop worrying about it. – Kevin Bourrillion Feb 28 '13 at 16:27
  • 2
    What if you're writing code for non-web apps, like tools to import data from a flat file (someone generates a file from code w/a bug and you get a data field like " " <-- tabs and spaces in there). I'm just pointing out that not all code is pre-sanitized for you, and `isBlank` is great for those use cases. – benson Mar 09 '13 at 01:30
  • 3
    @benson But is it really? Why would you want to trim the space out of " " so as to detect that it's empty, but preserve the space in " dog "? Assuming you can conjure up a reason, how *often* is that what you want? I maintain that `isBlank` is the most overused library method in history. :-) – Kevin Bourrillion Mar 09 '13 at 15:15
  • 2
    @KevinBourrillion I suppose you will want is as often as you are dealing with legacy code that depends on that kind of checking, and you are trying to make that code easier to read. – Adam Adamaszek Mar 13 '13 at 10:48
  • 14
    Everytime I check for something about methods that do not exist in Guava, @KevinBourrillion seems to have an answer like "nobody needs this" or "it should not be this way". People are not always working in a perfect world, and may need to protect their data against crap. You know, when you're dealing with a business service that is used by dozens of beginners? or dealing with a batch that processes a flat file coming out of 50 year old IBM mainframe programer who was padding his zones with spaces before you were born, and doesn't give a f about Java ? that's real life dude, `isBlank` is useful – Michael Técourt Dec 13 '13 at 22:13
  • @MichaelakoTecourt In most of those cases we have *still* not found `isBlank` to be the best way to handle it. Sure, you may sometimes want it, but that in itself doesn't obligate Guava to be the thing that provides it for you. – Kevin Bourrillion Feb 21 '14 at 16:28
  • @KevinBourrillion the reality is that most programmers now just write `Strings.nullToEmpty(str).trim().isEmpty()` because you know programmers are lazy & guava is still cool – bernstein Sep 16 '16 at 15:19
  • As @MichaelTécourt remarks, you can be working on a backend API where you have to validate input from any number of third party integrators. Even if you have a "front end", there can be multiple implementations and better to stop unwanted input right at the ingress. In this age of cheap and rapid horizontal scaling, the cost is likely negligible. Lastly, it's much nicer to read a single method call that does the obviously wanted thing. I came to this SO post precisely because I inherited a bug caused by NOT having this. – AdrianVeidt Sep 16 '19 at 11:32
-5

Guava is more or less targeted towards being a 'next generation' replacement of Apache Commons. There really isn't much practical difference between using isBlank() vs isNullOrEmpty() beyond using one or the other consistently.

Matt Taylor
  • 1,166
  • 9
  • 12