从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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)