String StringBuffer StringBuilder
三者的异同
String:不可变,底层使用char[存储]
StringBuffer:可变,jdk1.0就有线程安全,效率低
StringBuilder:可变,jdk5.0,线程不安全,效率高
比较三者的效率:
String<StringBuffer<StringBulider
package string; import org.junit.Test; /* * 关于StringBuffer的常用方法 * 增:append() * 删:delete(int start ,int end) * 改:setCharAt(int n,char ch),replace(int start,int end,String str) * 查:charAt() * 插:insert() * 长度:length() * 遍历:for+cahrAt()/toString * */ public class TestOne { @Test public void test1() { System.out.println("***********测试StringBuffer******************"); StringBuffer sb1=new StringBuffer(); sb1.append(1234).append("abcd"); System.out.println(sb1.toString()); sb1.delete(1, 3); System.out.println(sb1.toString()); sb1.setCharAt(2, 'm'); System.out.println(sb1.toString()); sb1.insert(3, true); System.out.println(sb1.toString()); System.out.println(sb1.length()); System.out.println("***************测试StringBuilder********************"); StringBuilder sl1=new StringBuilder(); sl1.append(4567).append("efgh"); sl1.insert(0, 'c'); System.out.println(sl1.toString()); //字符串是length()方法 System.out.println("******************测试String字符串*****************"); String s1=new String(); s1="opq"; System.out.println(s1.contains(sb1));//flase System.out.println(s1.charAt(1));//p System.out.println(s1);//opq System.out.println(s1.length());//3 //数组是length属性 System.out.println("******************测试数组*****************"); int[] int1=new int[] {1,2,3}; System.out.println(int1.length); }