Java-23 StringBuffer类
1.StringBuffer类概述:线程安全的可变字符序列
public final class StringBuffer
1.为什么存在?
如果对字符串进行拼接操作,每次拼接,都会构建一个新的String对象,既耗时,又浪费空间。而StringBuffer就可以解决这个问题
2.StringBuffer类和String类的区别:
1、前者的长度和内容都是可变的,后者都不可变
2、前者可以提前给出一个缓冲区,可以进行字符串的拼接,而不会重新开辟新的空间,而后者是会开辟新空间
后者,每创建新的字符串都会开辟新的空间
3.拓展:
线程安全与线程不安全
1、安全,代表着有同步操作出现,数据是安全的,效率不高(一个一个来)
2、不安全,不同步,效率高,(可以多个一起)
3、安全问题和效率问题一直都是头疼的问题
4、生活中哪些是线程安全的案例:
5、举例:
线程安全:银行的一些业务,售票,医院取号。
不是安全问题的,效率高的:博客,视频会员。
2.构造方法:
public StringBuffer()
public StringBuffer(int capacity)
public StringBuffer(String str)
/* StringBuffer的构造方法: StringBuffer() 构造一个没有字符的字符串缓冲区,初始容量为16个字符。 StringBuffer(int capacity) 构造一个没有字符的字符串缓冲区和指定的初始容量。 StringBuffer(String str) 构造一个初始化为指定字符串内容的字符串缓冲区。 StringBuffer的方法: public int capacity() 返回当前容量。 容量是新插入字符可用的存储量,超过此值将进行分配。 public int length() 返回长度(字符数)。 */ public class StringBuffer1 { public static void main(String[] args) { // StringBuffer() 构造一个没有字符的字符串缓冲区,初始容量为16个字符。 StringBuffer sb = new StringBuffer(); System.out.println(sb);//重写了toString()方法 System.out.println(sb.capacity());//16 System.out.println(sb.length());//0 //StringBuffer(int capacity) 构造一个没有字符的字符串缓冲区和指定的初始容量。 StringBuffer sb1 = new StringBuffer(500); System.out.println(sb1); System.out.println(sb1.capacity());//500 System.out.println(sb1.length());//0 //StringBuffer(String str) 构造一个初始化为指定字符串内容的字符串缓冲区。 StringBuffer sb3 = new StringBuffer("hellobigdata"); System.out.println(sb3);//hellobigdata System.out.println(sb3.capacity());//28 16+12 System.out.println(sb3.length());//12 } }
3.成员方法
1.添加功能:
public StringBuffer append(String str)
可以把任意类型的数据添加到字符串缓冲区中
返回的是当前字符串缓冲区本身
public StringBuffer insert(int offset,String str)
在指定的索引上,插入字符串
public class StringBuffer2 { public static void main(String[] args) { StringBuffer sb = new StringBuffer(); StringBuffer hello = sb.append("hello"); System.out.println(sb);//hello System.out.println(hello);//hello System.out.println(sb==hello);//true sb.append(1); sb.append('a'); sb.append(12.25); sb.append(false); System.out.println(sb);//hello1a12.25false System.out.println(hello);//hello1a12.25false //链式编程 sb.append("hello").append(1).append('a').append(12.25).append(false); System.out.println(sb);//hello1a12.25falsehello1a12.25false //public StringBuffer insert(int index,String str) //在指定的索引上,插入字符串 sb.insert(0,"IIII"); System.out.println(sb);//IIIIhello1a12.25falsehello1a12.25false System.out.println(hello);//IIIIhello1a12.25falsehello1a12.25false // sb.insert(50,29);//StringIndexOutOfBoundsException // System.out.println(sb); } }
2.删除功能
public StringBuffer deleteCharAt(int index)
public StringBuffer delete(int start,int end)
public class StringBuffer3 { public static void main(String[] args) { StringBuffer sb = new StringBuffer(); sb.append("hello").append(520).append("bigdata"); System.out.println(sb);//hello520bigdata //public StringBuffer deleteCharAt(int index)删除char在这个序列中的指定位置。 //返回的内容是本身 sb.deleteCharAt(5); System.out.println(sb);//hello20bigdata //System.out.println(sb.deleteCharAt(15));//StringIndexOutOfBoundsException //public StringBuffer delete(int start,int end) // 删除此序列的子字符串中的字符。 // 子串开始于指定start并延伸到字符索引end - 1 , [start,end) // 或如果没有这样的字符存在的序列的结束。 如果start等于end ,则不作任何更改。 System.out.println(sb.delete(1,1));//hello20bigdata System.out.println(sb.delete(0,7));//bigdata System.out.println(sb.delete(0,sb.length()));// } }
3.替换功能
public StringBuffer replace(int start,int end,String str)
/*替换功能 public StringBuffer replace(int start,int end,String str) 用指定的String中的字符替换此序列的子字符串中的String 。 这里的start和end表示的是原来的字符串需要替换的区间,含头不含尾 */ public class StringBuffer4 { public static void main(String[] args) { StringBuffer sb = new StringBuffer(); sb.append("hello").append(false); //public StringBuffer replace(int start,int end,String str) //这里的start和end表示的是原来的字符串需要替换的区间,含头不含尾 System.out.println(sb.replace(5,sb.length(),"true"));//hellotrue System.out.println(sb.replace(5,6,"true"));//hellotruerue } }
4.反转功能
public StringBuffer reverse()
public class StringBuffer5 { public static void main(String[] args) { StringBuffer sb = new StringBuffer("helloworldBigdata"); //public StringBuffer reverse() //导致该字符序列被序列的相反代替。 //如果序列中包含任何替代对,则将它们视为单个字符进行反向操作。 System.out.println(sb.reverse());//atadgiBdlrowolleh } }
5.截取功能
public String substring(int start)
public String substring(int start,int end)
/*截取功能 String substring(int start) 返回一个新的 String ,其中包含此字符序列中当前包含的字符的子序列。 String substring(int start, int end) 返回一个新的 String ,其中包含此序列中当前包含的字符的子序列。*/ public class StringBuffer6 { public static void main(String[] args) { StringBuffer sb = new StringBuffer(); sb.append("lycc").append(2016).append(1224); System.out.println(sb);//lycc20161224 //String substring(int start) //返回一个新的 String ,其中包含此字符序列中当前包含的字符的子序列。 String s1 = sb.substring(4); System.out.println(s1);//20161224 System.out.println(sb);//lycc20161224 //String substring(int start, int end) //返回一个新的 String ,其中包含此序列中当前包含的字符的子序列。 String s2 = sb.substring(0, 4); System.out.println(sb);//lycc20161224 System.out.println(s2);//lycc } }
注意事项:
截取功能和前面几个功能的不同: 返回值类型是String类型,本身没有发生改变