How to properly trim whitespaces from a string in Java?
Solution 1:
Google has made guava-libraries available recently. It may have what you are looking for:
CharMatcher.inRange('\0', ' ').trimFrom(str)
is equivalent to String.trim(), but you can customize what to trim, refer to the JavaDoc.
For instance, it has its own definition of WHITESPACE which differs from the JDK and is defined according to the latest Unicode standard, so what you need can be written as:
CharMatcher.WHITESPACE.trimFrom(str)
Solution 2:
I swear I only saw this after I posted the question: Google just released Guava, a library of core Java utilities.
I haven't tried this yet, but from what I can tell, this is fully Unicode compliant:
String s = " \t testing \u00a0"
s = CharMatcher.WHITESPACE.trimFrom(s);