Anyone know of a library that has a lot of useful CharSequences
It seems strange开发者_运维技巧 that no helper named something like CharSequences is available in the JDK when there are so many obvious utility implementations.
- repeating character
- char[] - yes i know about CharBuffer.wrap
- padding - wrap another CharSequence and pad - obviously both left and right versions
- trimming - trim a CharSequence left or right of whitespace.
- quoting - add quotes around another CharSequence.
etc.
Feel free to expand the list if you wish...
The API is not exactly what you describe and not all of the functons are available, but the Guava class CharMatcher
provides part of it and some more niceties.
CharMatcher.DIGIT.trimFrom(input)
CharMatcher.WHITESPACE.trimLeadingFrom(input)
CharMatcher.anyOf("aeiou").removeFrom(input)
CharMatcher.inRange('a','z').matchesAllOf(input)
Check StringUtils in the apache commons lang library http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/StringUtils.html
Neither Guava nor Apache Commons / Lang (nor the JRE library itself) has functionality like this based on the CharSequence
interface.
One could imagine Guava having a CharSequences
class for methods like this, but unfortunately several issues that requested for methods like this have already been rejected.
There's a library that kind-of takes the other approach: It provides implemntations of CharSequence
that are optimized for some specific use cases: Ropes for Java
If you use Rope
s in your code, you can tweak the specific implementations to be used to speed up regular operations (concatenation, removing sub-CharSequence
es, ...).
Part of the problem is that lots of the jdk api methods that could take CharSequence only take Strings. therefore, the usefulness of CharSequence is limited (and many of the impls that do take CharSequences just call toString()
anyway, negating much of the benefit).
that said, it is certainly a useful idea. could use the repeating version right now.
精彩评论