public class 灵活字符串 {
public static void main(String[] args) {
//创建一个字符串
StringBuffer a=new StringBuffer();//默认创建了一个16个长的
//通过构造器指定大小
StringBuffer b=new StringBuffer(100);
//通过给一个string 创建大小
StringBuffer c=new StringBuffer("hello");
System.out.println(c.charAt(1));//e
System.out.println(c.length());//5
//将String 转化成StringBuffer
//1、通过构造器
String s1="hello,tom";
StringBuffer d=new StringBuffer(s1);//返回的值给了d 对s1无影响
//2、使用append方法
StringBuffer e=new StringBuffer();
e=e.append(s1);
//将StringBuffer 转化成String
//1、使用toString 方法
StringBuffer f=new StringBuffer("韩顺平教育");
String s2=f.toString();
//2、用构造器
String s3 =new String(f);
//StringBuffer 常见的使用方法
StringBuffer s=new StringBuffer("hello");
//增
s.append(',');
s.append("张三丰");
s.append("赵敏").append(100).append(true).append(10.5);
System.out.println(s);//hello,张三丰赵敏100true10.5
//删
s.delete(11, 14);//删除索引为[11,14)的字符
System.out.println(s);//hello,张三丰赵敏true10.5
//改
s.replace(9, 11, "周芷若");//使用周芷若替换索引为(9.11)的字符
System.out.println(s);//hello,张三丰周芷若true10.5
//查
int weizhi=s.indexOf("555");//查找指定的字串在字符串中第一次出现的索引位置。找不到返回-1;
System.out.println(weizhi);
//插
s.insert(9, "赵敏");
System.out.println(s);//hello,张三丰赵敏周芷若true10.5
}
}
StringBuffer 相关的使用方法
最新推荐文章于 2024-05-18 16:42:38 发布