StringBuffer: 可变的字符序列,可以看作一个存储字符的一个容器
构造方法:
public StringBuffer()
public StringBuffer(int capacity)
public StringBuffer(String str)
点击查看代码
public class StringBufferDemo1 {
public static void main(String[] args) {
//public StringBuffer() 创建默认大小的StringBuffer容器
StringBuffer sb1 = new StringBuffer();
// //获取默认StringBuffer容器的大小
System.out.println("StringBuffer默认的容量大小为:" + sb1.capacity()); // 16
// System.out.println("sb1: " + sb1);
System.out.println("StringBuffer实际存储字符数量:"+ sb1.length()); // 0
//public StringBuffer(int capacity) 创建指定大小容量的StringBuffer
StringBuffer sb2 = new StringBuffer(50);
System.out.println("StringBuffer默认的容量大小为:" + sb2.capacity()); // 50
System.out.println("sb1: " + sb2);
System.out.println("StringBuffer实际存储字符数量:" + sb2.length()); // 0
//public StringBuffer(String str) 创建默认大小的StringBuffer容器,其中存储了一个字符串
StringBuffer sb3 = new StringBuffer("hello");
System.out.println("StringBuffer默认的容量大小为:" + sb3.capacity()); // 21
System.out.println("sb1: " + sb3);
System.out.println("StringBuffer实际存储字符数量:" + sb3.length()); // 5
}
}
** StringBuffer中的成员方法:**
添加功能
public StringBuffer append(String str)
public StringBuffer insert(int offset,String str)
删除功能
public StringBuffer deleteCharAt(int index)
public StringBuffer delete(int start,int end)
替换功能
public StringBuffer replace(int start,int end,String str)
反转功能
public StringBuffer reverse()
添加功能
public StringBuffer append(String str)
public StringBuffer insert(int offset,String str)
1.public StringBuffer append(String str) 在StringBuffer末尾处添加新的字符串
点击查看代码
//public StringBuffer append(String str) 在StringBuffer末尾处添加新的字符串
// StringBuffer sb2 = sb.append(100);
// System.out.println("sb: "+sb);
// System.out.println("sb2: "+sb2);
// System.out.println(sb==sb2);
sb.append(100);
sb.append(12.343);
sb.append(true);
System.out.println("sb: " + sb);
System.out.println("------------------------");
2.public StringBuffer insert(int offset,String str) 在StringBuffer指定位置中添加字符串
点击查看代码
//public StringBuffer insert(int offset,String str) 在StringBuffer指定位置中添加字符串
//10012.343true
sb.insert(10,"java");
System.out.println("sb: " + sb);
System.out.println("------------------------");
3.public StringBuffer deleteCharAt(int index) 指定索引删除StringBuffer某一个字符
点击查看代码
//public StringBuffer deleteCharAt(int index) 指定索引删除StringBuffer某一个字符
//10012.343tjavarue
sb.deleteCharAt(5);
System.out.println("sb: " + sb);
System.out.println("------------------------");
4.public StringBuffer delete(int start,int end) 指定开始和结束索引,删除StringBuffer一部分字符
点击查看代码
//public StringBuffer delete(int start,int end) 指定开始和结束索引,删除StringBuffer一部分字符
//10012343tjavarue
sb.delete(9,13); //[start, end)
System.out.println("sb: " + sb);
System.out.println("------------------------");
5.public StringBuffer replace(int start,int end,String str) 使用字符串替换StringBuffer一部分字符
点击查看代码
//public StringBuffer replace(int start,int end,String str) 使用字符串替换StringBuffer一部分字符
//10012343true
sb.replace(6,9,"李刚");
System.out.println("sb: " + sb);
System.out.println("------------------------");
6.public StringBuffer reverse()反转功能
点击查看代码
//public StringBuffer reverse()
//100123李刚rue
sb.reverse();
System.out.println("sb: " + sb);
System.out.println("------------------------");
截取功能
public String substring(int start)
public String substring(int start,int end)
点击查看代码
public class StringBufferDemo3 {
public static void main(String[] args) {
StringBuffer sb1 = new StringBuffer("hello world java hadoop");
//public String substring(int start)
String s1 = sb1.substring(4);
System.out.println("sb1: "+sb1);
System.out.println("s1: "+s1);
//public String substring(int start,int end)
String s2 = sb1.substring(6, 11);
System.out.println("sb1: "+sb1);
System.out.println("s2: "+s2);
}
}
String和StringBuffer的相互转换
数据类型之间相互转换的场景:
1、方法传参所需的类型与我自己值的类型不一样
2、需要借助其它类型中的方法完成某功能
点击查看代码
public class StringBufferDemo4 {
public static void main(String[] args) {
String s1 = "hello";
//String -> StringBuffer
StringBuffer sb1 = new StringBuffer(s1);
//StringBuffer->String
String s2 = sb1.substring(0);
String s3 = sb1.toString();
}
}