记录生活中的点滴,分享、学习、创新
1、StringBuffer类概述
1)我们如果对字符串进行拼接操作,每次拼接,都会构建一个新的String对象,既耗时,又浪费空间。而StringBuffer就可以解决这个问题
2)线程安全的可变字符序列
3)StringBuffer和String的区别
· 前者长度和内容可变,后者不可变。
· 如果使用前者做字符串的拼接,不会浪费太多的资源。
2、构造方法
· public StringBuffer() :无参构造方法
· public StringBuffer(int capacity) :指定容量的字符串缓冲区对象
· public StringBuffer(String str) :指定字符串内容的字符串缓冲区对象
StringBuffer的方法:
· public int capacity():返回当前容量,理论值
· public int length():返回长度(字符数) ,实际值
public class StringBufferDemo01 { public static void main(String[] args) { // public StringBuffer():无参构造方法 StringBuffer sb = new StringBuffer(); System.out.println("sb:"+sb); System.out.println("sb.capacity:"+sb.capacity()); //16 System.out.println("sb.length:"+sb.length()); //0 System.out.println("--------------------------"); // public StringBuffer(int capacity):指定容量的字符串缓冲区对象 StringBuffer sb2 = new StringBuffer(50); System.out.println("sb2:"+sb2); System.out.println("sb2.capacity:"+sb2.capacity()); //50 System.out.println("sb2.length:"+sb2.length()); //0 System.out.println("--------------------------"); // public StringBuffer(String str):指定字符串内容的字符串缓冲区对象 StringBuffer sb3 = new StringBuffer("hello"); System.out.println("sb3:"+sb3); //"hello" System.out.println("sb3.capacity:"+sb3.capacity()); //21 System.out.println("sb3.length:"+sb3.length());//5 System.out.println("--------------------------"); } }
注意返回值,可以查看源码,默认空间是16。
/** * Constructs a string buffer with no characters in it and an * initial capacity of 16 characters. */ public StringBuffer() { super(16); } /** * Constructs a string buffer with no characters in it and * the specified initial capacity. * * @param capacity the initial capacity. * @exception NegativeArraySizeException if the <code>capacity</code> * argument is less than <code>0</code>. */ public StringBuffer(int capacity) { super(capacity); } /** * Constructs a string buffer initialized to the contents of the * specified string. The initial capacity of the string buffer is * <code>16</code> plus the length of the string argument. * * @param str the initial contents of the buffer. * @exception NullPointerException if <code>str</code> is <code>null</code> */ public StringBuffer(String str) { super(str.length() + 16); append(str); }
3、StringBuffer类的成员方法 1)添加功能
· public StringBuffer append(String str):可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
· public StringBuffer insert(int offset,String str):在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
public class StringBufferDemo02 { public static void main(String[] args) { // 创建字符串缓冲区对象 StringBuffer sb = new StringBuffer(); //返回对象本身 StringBuffer sb2 = sb.append("hello"); System.out.println("sb:"+sb); //sb:hello System.out.println("sb2:"+sb2); //sb2:hello System.out.println(sb == sb2); //true //一步一步的添加数据 StringBuffer sb3 = new StringBuffer(); sb3.append("hello"); sb3.append(true); sb3.append(12); sb3.append(34.56); System.out.println("sb3:"+sb3); //sb3:hellotrue1234.56 //链式编程 StringBuffer sb4 = new StringBuffer(); sb4.append("hello").append(true).append(12).append(34.56); System.out.println("sb4:"+sb4); //sb4:hellotrue1234.56 //StringBuffer insert(int offset,Stringstr):插入数据 sb3.insert(5,"hello"); System.out.println("sb3:"+sb3); //sb3:hellohellotrue1234.56 } }
· public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身
· public StringBuffer delete(int start,int end):删除从指定位置开始指定位置结束的内容,并返回本身
public class StringBufferDemo03 { public static void main(String[] args) { // 创建对象 StringBuffer sb1 = new StringBuffer(); // 创建对象 sb1.append("hello").append("world").append("java"); System.out.println("sb1:"+sb1); //sb1:helloworldjava // public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身 // 需求:我要删除e这个字符 sb1.deleteCharAt(1); // 需求:我要删除第一个l这个字符 sb1.deleteCharAt(1); System.out.println("sb1:"+sb1); //sb1:hloworldjava System.out.println("----------------"); // public StringBuffer delete(int start,int end):删除从指定位置开始指定位置结束的内容,并返回本身 StringBuffer sb2 = new StringBuffer("Hello World Java"); System.out.println("sb2:"+sb2); //sb2:Hello World Java // 需求:我要删除World这个字符串 sb2.delete(5,11); System.out.println("sb2:"+sb2); //sb2:Hello Java // 需求:我要删除所有的数据 sb2.delete(0, sb2.length()); System.out.println("sb2:"+sb2); //sb2: } }
· public StringBuffer replace(int start,int end,String str):使用给定String中的字符替换词序列的子字符串中的字符
public class StringBufferDemo04 { public static void main(String[] args) { // 创建字符串缓冲区对象 StringBuffer sb = new StringBuffer(); // 添加数据 sb.append("hello"); sb.append("world"); sb.append("java"); System.out.println("sb:" + sb); //sb:helloworldjava // public StringBuffer replace(int start,int end,String str):从start开始到end用str替换 // 需求:我要把world这个数据替换为"节日快乐" sb.replace(5,10,"节日快乐"); System.out.println("sb:"+sb); //sb:hello节日快乐java } }
· public StringBuffer reverse():将此字符序列用其反转形式取代,返回对象本身
public class StringBufferDemo05 { public static void main(String[] args) { //创建字符串缓冲区对象 StringBuffer sb = new StringBuffer(); //添加数据 sb.append("林青霞爱我"); System.out.println("sb:"+sb); //sb:林青霞爱我 //public StringBuffer reverse() sb.reverse(); System.out.println("sb:"+sb); //sb:我爱霞青林 } }
· public String substring(int start):返回一个新的String,它包含此字符序列当前所包含的字符子序列
· public String substring(int start,int end):返回一个新的String,它包含此序列当前所包含的字符子序列
注意:截取功能和前面几个功能的不同
· 返回值类型是String类型,本身没有发生改变
public class StringBufferDemo06 { public static void main(String[] args) { //创建字符串缓冲区对象 StringBuffer sb = new StringBuffer(); sb.append("hello").append("world").append("java"); System.out.println("sb:"+sb); //sb:helloworldjava //截取功能 String s = sb.substring(5); System.out.println("s:"+s); //s:worldjava System.out.println("sb:"+sb); //sb:helloworldjava String ss = sb.substring(5,10); System.out.println("ss:"+ss); //ss:world System.out.println("sb:"+sb); //sb:helloworldjava } }
4、练习题:String与StringBuffer的相互转换
public class StringBufferDemo07 { public static void main(String[] args) { //String --> StringBuffer String s = "hello"; // 注意:不能把字符串的值直接赋值给StringBuffer // StringBuffer sb = "hello"; // StringBuffer sb = s; //方式一:通过构造方法 StringBuffer sb = new StringBuffer(s); //方式二:通过append方法 StringBuffer sb2 = new StringBuffer(); sb2.append(s); System.out.pr