当我比较Apache的性能StringUtils.replace()VS String.replace()我很惊讶地知道,前者是快约4倍.我使用Google的Caliper框架来衡量效果.这是我的考试
public class Performance extends SimpleBenchmark {
String s = "111222111222";
public int timeM1(int n) {
int res = 0;
for (int x = 0; x < n; x++) {
res += s.replace("111", "333").length();
}
return res;
}
public int timeM2(int n) {
int res = 0;
for (int x = 0; x < n; x++) {
res += StringUtils.replace(s, "111", "333", -1).length();
}
return res;
}
public static void main(String... args) {
Runner.main(Performance.class, args);
} …Run Code Online (Sandbox Code Playgroud) 目前我org.apache.commons.lang.StringEscapeUtils escapeHtml()用来逃避我的字符串中不需要的HTML标签,但后来我意识到它也带有重音符号&something;,,这是我不想要的.
你是否知道任何逃避HTML标签的解决方案,但留下我的特别(好吧,对某些人来说,这里是正常的;])这些字母是什么?
提前致谢!
巴拉兹
是否有任何理由要使用String.replaceAll(String,String)而不是StringUtils.replace()全部使用?
假设两个库都可用,并且我们没有传递正则表达式。
从关于这一主题的几个以前的职位,我看到有多个测试和基准指着那String.replace是缓慢的,但我不记得任何人解释是否有使用的情况下,String.replaceAll在StringUtils.replace