Home > Java > Remember to check for capitalized spaces

Remember to check for capitalized spaces

VN:R_U [1.6.3_896]
Rating: 5.0/5 (1 vote cast)

Saw this little gem in some code recently:

 if (someString != null
   && !someString.equalsIgnoreCase("")
   && !someString.equalsIgnoreCase(" ")) { ... }

Although the author of this code was lax in checking for cases where someString had more than one space, he/she was mindful enough to check for those pesky capitalized spaces.

Seriously folks, Apache Commons Lang (http://commons.apache.org/) has a lot of great stuff ready-made for these commons problems. Just check out StringUtils.isNotBlank() for a better, more complete, and more readable solution:

 if (StringUtils.isNotBlank(someString)) { ... }

Using static imports, this reads even nicer:

if (isNotBlank(someString)) { ... }
VN:R_U [1.6.3_896]
Rating: 5.0/5 (1 vote cast)

Categories: Java Tags:
  1. October 15th, 2009 at 14:28 | #1

    If only it weren’t for those pesky lower-space cases always screwing things up. .NET has a baked-in String.IsNullOrEmpty() function that will test for a null or empty string. Does Java have similar functionality?

    VN:F [1.6.3_896]
    Rating: 0.0/5 (0 votes cast)
  1. No trackbacks yet.