Remember to check for capitalized spaces
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)) { ... }
Categories: Java

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?