Tuesday, September 17, 2013

Java String.trim() is dead

Ever had problems with spaces that were not removed by String.trim() ? 

This is because trim() only removes breaking white spaces. 

But sometimes, if you are copying from Microsoft products (like Note, for example) some white spaces get copied by default. These whitespaces are non-breaking white spaces, which will not be handled by trim().

To get around this, instead of using trim() use com.google.common.base.CharMatcher

return CharMatcher.WHITESPACE.trimFrom(" Hello   ");

returns "Hello" regardless of what kind of spaces are involved.

Also note that other java and Apache Commons methods like StringUtils.isBlank() might use only breaking white spaces. That is why I prefer to eschew these and use com.google.common.base where ever possible.

For a full list of white space chars, take a peek at http://www.theasciicode.com.ar/extended-ascii-code/non-breaking-space-no-break-space-ascii-code-255.html

No comments:

Post a Comment