Is there a substring proxy in scala?
Using strings as String objects is pretty convenient for many string processing tasks.
I need extract some substrings to process and scala String class provide me with such functionality. But it is rather expensive: new String object is created every time substring function is used. Using tuples (string : String, start : Int, stop : Int) solves the performance problem, but makes code much complicated.
Is there any library f开发者_如何学运维or creating string proxys, that stores original string, range bound and is compatibles with other string functions?
Java 7u6 and later now implement #substring
as a copy, not a view, making this answer obsolete.
If you're running your Scala program on the Sun/Oracle JVM, you shouldn't need to perform this optimization, because java.lang.String
already does it for you.
A string is stored as a reference to a char
array, together with an offset and a length. Substrings share the same underlying array, but with a different offset and/or length.
Look at the implementation of String
(in particular substring(int beginIndex, int endIndex)
): it's already represented as you wish.
精彩评论