Commons Lang StringUtils.replace性能vs String.replace

Commons Lang StringUtils.replace性能vs String.replace,第1张

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



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5020742.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存