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/archive/2011/07/27/2117917.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值