问题描述
JDK的 String.trim( )方法非常幼稚,只会删除ascii控制字符.
The JDK's String.trim() method is pretty naive, and only removes ascii control characters.
Apache Commons' StringUtils.strip()稍好一些,但使用了JDK的 Character.isWhitespace(),其中.
Apache Commons' StringUtils.strip() is slightly better, but uses the JDK's Character.isWhitespace(), which doesn't recognize non-breaking space as whitespace.
那么在Java中修剪字符串的最完整,与Unicode兼容,安全和正确的方法是什么?
So what would be the most complete, Unicode-compatible, safe and proper way to trim a string in Java?
顺便说一句,我应该使用比commons-lang
更好的库吗?
And incidentally, is there a better library than commons-lang
that I should be using for this sort of stuff?
推荐答案
Google has made guava-libraries available recently. It may have what you are looking for:
CharMatcher.inRange('\0', ' ').trimFrom(str)
等效于String.trim(),但是您可以自定义要修剪的内容,请参考JavaDoc.
is equivalent to String.trim(), but you can customize what to trim, refer to the JavaDoc.
例如,它具有它自己的WHITESPACE定义与JDK不同,并且是根据最新的Unicode标准定义的,因此所需的内容可以写为:
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)
这篇关于如何在Java中正确修剪字符串中的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!