如何用String实现StringBuilder.replace(String,String)? [英] How to implement StringBuilder.replace(String, String) in terms of String?

查看:171
本文介绍了如何用String实现StringBuilder.replace(String,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 Strings 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆