public class StringTest {
/**
* @desc 使用+、concat()、append()方法循环10W次
* @author chenssy
* @data 2013-11-16
* @param args
* @return void
*/
public static void main(String[] args) {
//+
long start_01 = System.currentTimeMillis();
String a = "a";
for(int i = 0 ; i < 100000 ; i++){
a += "b";
}
long end_01 = System.currentTimeMillis();
System.out.println(" + 所消耗的时间:" + (end_01 - start_01) + "毫米");
//concat()
long start_02 = System.currentTimeMillis();
String c = "c";
for(int i = 0 ; i < 100000 ; i++){
c = c.concat("d");
}
long end_02 = System.currentTimeMillis();
System.out.println("concat所消耗的时间:" + (end_02 - start_02) + "毫米");
//append
long start_03 = System.currentTimeMillis();
StringBuffer e = new StringBuffer("e");
for(int i = 0 ; i < 100000 ; i++){
e.append("d");
}
long end_03 = System.currentTimeMillis();
System.out.println("append所消耗的时间:" + (end_03 - start_03) + "毫米");
}
}
------------
Output:
+ 所消耗的时间:19080毫米
concat所消耗的时间:9089毫米
append所消耗的时间:10毫米
public AbstractStringBuilder append(String str) {
if (str == null) str = "null";
int len = str.length();
if (len == 0) return this;
int newCount = count + len;
if (newCount > value.length)
expandCapacity(newCount);
str.getChars(0, len, value, count);
count = newCount;
return this;
}
这是concat()的源码,它看上去就是一个数字拷贝形式,我们知道数组的处理速度是非常快的,但是由于该方法最后是这样的:return new String(0, count + otherLen, buf);这同样也创建了10W个字符串对象,这是它变慢的根本原因。
(三)append()方法拼接字符串
public synchronized StringBuffer append(String str)
{
super.append(str);
return this;
}
public AbstractStringBuilder append(String str)
{
if (str == null) str = "null";
int len = str.length();
if (len == 0) return this;
int newCount = count + len;
if (newCount > value.length)
expandCapacity(newCount);
str.getChars(0, len, value, count);
count = newCount;
return this;
}
与concat()方法相似,它也是进行字符数组处理的,加长,然后拷贝,但是请注意它最后是返回并没有返回一个新串,而是返回本身,也就说这这个10W次的循环过程中,它并没有产生新的字符串对象。
通过上面的分析,我们需要在合适的场所选择合适的字符串拼接方式,但是并不一定就要选择append()和concat()方法,原因在于+根据符合我们的编程习惯,只有到了使用append()和concat()方法确实是可以对我们系统的效率起到比较大的帮助,才会考虑。
以上内容转自:http://blog.csdn.net/chenssy/article/details/17591363
StringBuffer str = new StringBuffer("abde");
String strTest = str.toString();
StringBuffer str = new StringBuffer("abde").append("zhongguo");
String strTest = str.toString();
String str = new StringBuffer().append("ab").append("中国").append(1123).to.String();
package he.qiang;
import java.lang.String;
public class StringTest
{
public static void main(String[] args)
{
StringBuffer strr = "hdede";
}
}
Constructs a string buffer with no characters in it and an initial capacity of 16 characters.默认的长度的16字符
//创建一个不带内容的StringBuffer对象:
StringBuffer str = new StringBuffer();
//创建一个StringBuffer对象str,且该对象初始化为一个空的对象。
Constructs a string buffer initialized to the contents of the specified string.
//创建一个带内容的StringBuffer对象:
StringBuffer str = new StringBuffer("china");
//创建一个StringBuffer对象str,且该对象的内容被初始化为"china"。
构造方法StringBuffer(CharSequence seq)
—–Constructs a string buffer that contains the same characters as the specified CharSequence.
构造方法StringBuffer(int capacity)
—–Constructs a string buffer with no characters in it and the specified initial capacity.
package he.qiang;
import java.lang.String;
public class StringTest
{
public static void main(String[] args)
{
StringBuffer str1 = new StringBuffer();
StringBuffer str2 = new StringBuffer(10);
StringBuffer str3 = new StringBuffer(20);
System.out.println(str1.capacity());//运行结果:16
System.out.println(str2.capacity());//运行结果:10
System.out.println(str3.capacity());//运行结果:20
}
}
StringBuffer str = "abc"; //赋值类型不匹配
StringBuffer s = (StringBuffer)str; //不存在继承关系,无法进行强转
String s = "abc";
StringBuffer sb2 = new StringBuffer(s); //String转换为StringBuffer
(1)StringBuffer sb1 = new StringBuffer("abc");
(2)String s1 = sb1.toString(); //StringBuffer转换为String
//可以将(1)(2)写合一句:String s1 = new StringBuffer("abc").toString();
在StringBuffer的使用方面,它更加侧重于对字符串的变化,例如追加、修改、删除,相对应的方法:
(1)append():追加指定内容到当前StringBuffer对象的末尾,类似于字符串的连接,这里StringBuffer对象的内容会发生改变。
(2)insert:该类方法主要是在StringBuffer对象中插入内容。
(3)delete:该类方法主要用于移除StringBuffer对象中的内容。
以下部分内容转载出处:http://www.cnblogs.com/springcsc/archive/2009/12/03/1616330.html
a)append方法:public StringBuffer append(boolean b)
该方法的作用是将Boolean参数作为内容追加到当前StringBuffer对象的末尾,类似于字符串的连接。调用该方法以后,StringBuffer对象的内容也发生改变,例如:
StringBuffer sb = new StringBuffer("abc");
sb.append(true);
//则对象sb的值将变成"abctrue"。
StringBuffer sb = new StringBuffer();
String user = "test";
String pwd = "123";
sb.append("select * from userInfo where username=").append(user).append(" and pwd=").append(pwd);
//这样对象sb的值就是字符串"select * from userInfo where username=test and pwd=123"。
StringBuffer sb = new StringBuffer("Test");
sb. deleteCharAt(1);
该代码的作用删除字符串对象sb中索引值为1的字符,也就是删除第二个字符,剩余的内容组成一个新的字符串。所以对象sb的值变为“Tst”
。
StringBuffer sb = new StringBuffer("TestString");
sb. delete (1,4);
该代码的作用是删除索引值1(包括)到索引值4(不包括)之间的所有字符,变量sb指向的内容为剩余的字符。则对象sb的值是”TString”。
StringBuffer sb = new StringBuffer(“TestString”);
sb.insert(4,false);
该示例代码的作用是在对象sb的索引值4的位置插入false值,形成新的字符串,则执行以后对象sb的值是”TestfalseString”。
StringBuffer sb = new StringBuffer(“abc”);
sb.reverse();
//经过反转以后,对象sb中的内容将变为”cba”。
StringBuffer sb = new StringBuffer("abc");
sb.setCharAt(1,'D');
//则对象sb的值将变成"aDc"。
package he.qiang;
import java.lang.String;
public class StringTest
{
public static void main(String[] args)
{
StringBuffer str1 = new StringBuffer();//初始容量是16个字符
str1.append("abc");
System.out.println(str1.capacity());
int strLength = str1.length();
/*the length of the sequence of characters currently represented by this object*/
System.out.println(strLength);
str1.trimToSize();
System.out.println(str1.capacity());
}
}
- g)substring方法:public String substring(int start)和public String substring(int start, int end)
该方法的作用和String类中的substring方法一样。