StringBuilder使用方法

 1 public class StringBuilderDemo {
2
3 /**
4 * @param args
5 */
6 public static void main(String[] args) {
7 //
8 // StringBuilder buf=new StringBuilder();
9 // buf.append("李敖").append(":").
10 // append("是著名的国学大师,前妻是胡女士").
11 // insert(0, "大师").replace(1, 2, "牛人");
12 // String str=buf.toString();
13 // System.out.println(str);
14 System.out.println(testString(100000));
15 System.out.println(testStringBuilder(100000));
16 }
17 //计算:使用string 的连接运算(+)连接指定次数
18 // 的字符串性能
19 public static long testString (int times){
20 //毫秒数
21 long start = System.currentTimeMillis();
22 String s="";
23 for(int i=0;i<times;i++){
24 s+="a";
25 }
26 long end = System.currentTimeMillis();
27 return end-start;
28 }
29 public static long testStringBuilder(int times){
30 long start = System.currentTimeMillis();
31 StringBuilder buf=new StringBuilder();
32 for(int i=0;i<times;i++){
33 buf.append("a");
34 }
35 //System.out.println(buf.toString());
36 long end = System.currentTimeMillis();
37 return end-start;
38 }
39 }

原文地址:https://www.cnblogs.com/superjt/p/2117917.html