StringBuffer 相关的使用方法

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
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值