原文件地址:http://www.cnblogs.com/focusj/archive/2011/02/16/2057630.html
package
test;
import
org.apache.commons.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;
}
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);
return
sb.toString();
}
public
void
testStringBuilder() {
StringBuilder sb =
new
StringBuilder();
for
(
int
i =
0
; i <
100000
; i++) {
sb.append(String.valueOf(i));
}
sb.toString();
}
public
static
void
main(String[] args) {
System.out.println(
new
CutString().testStringBuffer());
}
}