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

Commons Lang StringUtils.replace性能vs String.replace

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

Commons Lang StringUtils.replace性能vs String.replace

从1的源代码中:

java.lang.String

public String replace(CharSequence target, CharSequence replacement) {   return Pattern .compile(target.toString(), Pattern.LITERAL) .matcher(this ) .replaceAll(         Matcher.quoteReplacement(replacement.toString()));}

String.replace(CharSequence target, CharSequencereplacement)
与实施
java.util.regex.Pattern
,因此,它是不奇怪的是较慢的是2,这是与实现和。
StringUtils.replace(Stringtext, String searchString, Stringreplacement)
indexOf``StringBuffer

public static String replace(String text, String searchString, String replacement) {    return replace(text, searchString, replacement, -1);}public static String replace(String text, String searchString, String replacement, int max) {    if (isEmpty(text) || isEmpty(searchString) || replacement == null || max == 0) {        return text;    }    int start = 0;    int end = text.indexOf(searchString, start);    if (end == -1) {        return text;    }    int replLength = searchString.length();    int increase = replacement.length() - replLength;    increase = (increase < 0 ? 0 : increase);    increase *= (max < 0 ? 16 : (max > 64 ? 64 : max));    StringBuffer buf = new StringBuffer(text.length() + increase);    while (end != -1) {        buf.append(text.substring(start, end)).append(replacement);        start = end + replLength;        if (--max == 0) { break;        }        end = text.indexOf(searchString, start);    }    buf.append(text.substring(start));    return buf.toString();}

脚注

1我链接到并复制源代码的版本是JDK 7

2我链接到并从中复制源代码的版本是common-lang-2.5



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

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

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