java五种拼接字符串的⽅法package test;
import org.apachemons.lang.StringUtils;
public class CutString {
//性能最差
public String testPlus() {
String s = "";
for (int i = 0; i < 10000; i++) {
s = s + String.valueOf(i);
}
return s;
}
/
/较好
public String testConcat() {
String s = "";
for (int i = 0; i < 10000; i++) {
s = s.concat(String.valueOf(i));
}
return s;
}
//性能与testStringBuffer相差⽆⼏,⽐其要好
public String testJoin() {
String[] list = new String[10000];
字符串截取拼接不固定
for (int i = 0; i < 10000; i++) {
list[i] = String.valueOf(i);
}
return StringUtils.join(list, "");
}
public String testStringBuffer() {
StringBuffer sb = new StringBuffer();
long l1 = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
sb.append(String.valueOf(i));
}
long l2 = System.currentTimeMillis();
System.out.println(l2-l1);
String();
}
//性能最⾼的⽅法
public void testStringBuilder() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 100000; i++) {
sb.append(String.valueOf(i));
}
}
public static void main(String[] args) {
System.out.println(new CutString().testStringBuffer());
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。