自由选择字符串拼接方法

简介:

对一个字符串拼接有三种方法:加号,contact方法,StringBuffer或者StringBuilder的append方法,其中加号是最常用的.其他两种方式偶尔会出现在一些开源项目中,那么这三者有什么区别?

        //加号拼接
        str += "c";
        //concat方法连接
        str = str.concat("c");

以上是两种不同的字符串拼接方式,循环5万次后再检查执行的时间,加号方式执行的时间是1438毫秒,而concat方法的执行时间是703毫秒,时间相差一倍,如果使用StringBuilder方式,执行时间会更少.

复制代码
 1 public class Client {
 2     public static final int MAX_LOOP = 50000;
 3     public static void main(String[] args) {
 4         doWithPlus();
 5         doWithConcat();
 6         doWithStringBuffer();
 7         
 8         String str ="abc";
 9         String str1 = str.concat("1");
10         String str2 = "abc1";
11         System.out.println(str1 == str2);
12     }
13     
14     public static void doWithPlus(){
15         String str = "a";
16         long start = System.currentTimeMillis();
17         for(int i=0;i<MAX_LOOP;i++){
18             str += "c";
19             //str = new StringBuilder(prefix).append("c").toString();
20         }
21         long finish = System.currentTimeMillis();
22         System.out.println("doWithPlus:" + (finish - start) + "ms");
23     }
24     
25     public static void doWithConcat(){
26         String str = "a";
27         long start = System.currentTimeMillis();
28         for(int i=0;i<MAX_LOOP;i++){
29             str = str.concat("c");
30         }
31         long finish = System.currentTimeMillis();
32         System.out.println("doWithConcat:" + (finish - start) + "ms");
33     }
34     
35     public static void doWithStringBuffer(){
36         StringBuilder sb = new StringBuilder("a");
37         long start = System.currentTimeMillis();
38         for(int i=0;i<MAX_LOOP;i++){
39             sb.append("c");
40         }
41         String str = sb.toString();
42         long finish = System.currentTimeMillis();
43         System.out.println("doWithStringBuffer:" + (finish - start) + "ms");
44     }
45 }
复制代码

 

运行结果:

doWithPlus:1559ms
doWithConcat:748ms
doWithStringBuffer:2ms
false

 

StringBuffer的append方法的执行时间是0毫秒.说明时间非常的短(毫秒不足以计时,可以使用纳秒进行计算).这个实验说明在字符串拼接的方式中,append方法最快,concat方法次之,加号最慢,这是为何呢?

(1)"+"方法拼接字符串

虽然编译器对字符串的加号做了优化,它会使用StringBuilder的append方法进行追加,按道理来说,其执行时间应该也是0毫秒,不过它最终是通过toString方法转换成String字符串的,例子中"+"拼接的代码与如下代码相同:

str = new StringBuilder(str).append("c").toString();

 

它与纯粹的使用StrignBuilder的append方法是不同的,意思每次循环都会创建一个StringBuilder对象,二是每次执行完毕都要调用toString方法将其转换为字符串------它的时间都耗费在这里了.

(2)concat方法拼接字符串

复制代码
 1     public String concat(String str) {
 2         int otherLen = str.length();
 3         //如果追加的字符串长度为0,着返回字符串本身
 4         if (otherLen == 0) {
 5             return this;
 6         }
 7         int len = value.length;
 8         char buf[] = Arrays.copyOf(value, len + otherLen);
 9         //追加的字符串转化成字符数组,添加到buf中
10         str.getChars(buf, len);
11         //复制字符数组,产生一个新的字符串
12         return new String(buf, true);
13     }
复制代码

 

其整体看上去就是一个数组的拷贝,虽然在内存中的处理都是原子性操作,速度非常快,不过,注意看最后的return语句,每次的concat操作都会新创建一个String对象,这就是concat速度慢下来的真正原因,它创建了5万个String对象.

(3)append方法拼接字符串

StringBuilder的append方法直接由父类AbstractStringBuilder实现,其代码如下....

复制代码
    public AbstractStringBuilder append(String str) {
        if (str == null) str = "null";//如果是null值,则把null作为字符串处理
        int len = str.length();
        ensureCapacityInternal(count + len);//加长,并作数组拷贝
        str.getChars(0, len, value, count);
        count += len;
        return this;
    }
复制代码

 

整个append方法都在做字符组处理,加长,然后数组拷贝,这些都是基本数据处理,没有新建任何对象,所以速度也就最快了.

例子中是在最后通过StringBuffer的toString返回了一个字符串,也就是在5万次循环结束之后才生成了一个String对象.

 

"+"非常符合我们的编码习惯,适合人类阅读,在大多数情况下都可以使用加号操作,只有在系统性能临界的时候才考虑使用concat或者apped方法.

而且很多时候,系统的80%的系能消耗是在20%的代码上,我们的精力应该更多的投入到算法和结构上.

 


本文转自SummerChill博客园博客,原文链接:http://www.cnblogs.com/DreamDrive/p/5660256.html,如需转载请自行联系原作者

相关文章
|
8月前
字符串\字符串处理常用函数
字符串\字符串处理常用函数
61 2
|
9月前
String类及相应的字符串操作方法
String类及相应的字符串操作方法
110 1
两种字符串替换的方法
两种字符串替换的方法
108 0
|
人工智能 C#
C# 字符串拼接
除了可以通过加号来拼接字符串之外,我们还可以使用格式化字符串的方法来拼接字符串。中,字符串没有相加的数学运算,但它可以通过加号。是唯一可以用于字符串运算的算数运算符,别的。这样也可以实现字符串的拼接。也可以和复合赋值运算符。
166 0
C# 字符串拼接
|
存储 编译器
字符串操作函数详解
字符串操作函数详解
字符串操作函数详解
StringBuilder案例:字符串拼接
StringBuilder案例:字符串拼接
154 0
|
缓存 安全 Java
拼接字符串,还能这么玩
大家好,我是指北君。
拼接字符串,还能这么玩
【Groovy】字符串 ( 字符串拼接 | 多行字符串 )
【Groovy】字符串 ( 字符串拼接 | 多行字符串 )
535 0
【Groovy】字符串 ( 字符串拼接 | 多行字符串 )

热门文章

最新文章