栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用StringBuilder替换所有出现的String吗?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用StringBuilder替换所有出现的String吗?

好了,你可以编写一个循环:

public static void replaceAll(StringBuilder builder, String from, String to){    int index = builder.indexOf(from);    while (index != -1)    {        builder.replace(index, index + from.length(), to);        index += to.length(); // Move to the end of the replacement        index = builder.indexOf(from, index);    }}

请注意,在某些情况下

lastIndexOf
,从背面开始使用可能会更快。我怀疑是用短字符串替换长字符串的情况-因此,当你开始时,任何替换都很少复制。无论如何,这应该给你一个起点。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/402433.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号