开发者

Is there a substring proxy in scala?

开发者 https://www.devze.com 2023-04-05 03:11 出处:网络
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 r

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号