如何用String实现StringBuilder.replace(String,String)? [英] How to implement StringBuilder.replace(String, String) in terms of String?
问题描述
字符串包含一堆有用的方法,如 String.replace(CharSequence,CharSequence) lang / StringBuilder.htmlrel =nofollow noreferrer> StringBuilder 。有原因吗?
有没有一种简单的方法来实现这些方法,而没有调用 StringBuilder.toString()每次复制字符串?
自 StringBuilder
提供 indexOf(String,int)
和 replace(int,int,String)
可以轻松地重现该功能。这里唯一的缺点是参数不能是任何 CharSequence
对象,但必须是 String
s。 / p>
当处理巨大的类似字符串的对象并进行大量替换操作时,那么像可以使用Ropes for Java 。
String contains a bunch of useful methods such as String.replace(CharSequence, CharSequence) that are completely missing from StringBuilder. Is there a reason why?
Is there an easy way to implement these methods without the huge overhead of invoking StringBuilder.toString() which copies the string every time?
Since StringBuilder
provides both indexOf(String,int)
and replace(int,int,String)
one can easily reproduce the functionality. The only drawback here is that the arguments can't be any CharSequence
objects, but must be String
s instead.
When handling huge string-like objects and doing lots of replace operations, then a specialized API like Ropes for Java could be used.
这篇关于如何用String实现StringBuilder.replace(String,String)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!