StringBuffer的常见方法及注意事项

StringBuffer的添加功能

	public StringBuffer append(String str):
		可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
	public StringBuffer insert(int offset,String str):
		在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
		
	*StringBuffer是字符串缓冲区,当new的时候是在堆内存创建了一个对象,底层是一个长度为16的字符数组
	当调用添加的方法时,不会再重新创建对象,在不断向原缓冲区添加字符34:54
 

 StringBuffer的删除功能

   public StringBuffer deleteCharAt(int index) 
          Removes the char at the specified position in this sequence 
   public StringBuffer delete(int start,int end)
    	  Removes the characters in a substring of this sequence. 
    	  The substring begins at the specified start and extends to the character at index end - 1 
    	  or to the end of the sequence if no such character exists. If start is equal to end, no changes are made. 
          Removes the char at the specified position in this sequence 
   public StringBuffer delete(int start,int end)
    	  Removes the characters in a substring of this sequence. 
    	  The substring begins at the specified start and extends to the character at index end - 1 
    	  or to the end of the sequence if no such character exists. If start is equal to end, no changes are made. 
 

StringBuffer的替换功能

	* public StringBuffer replace(int start,int end,String str):
		* 从start开始到end用str替换  ,不包含尾


StringBuffer的反转功能

	* public StringBuffer reverse():
		* 字符串反转


 StringBuffer的截取功能及注意事项

 	public String substring(int start):
			* 从指定位置截取到末尾.包含开始位置即该指定位置
	public String substring(int start,int end):
			* 截取从指定位置开始到结束位置,包括开始位置,不包括结束位置
	注意事项
		* 注意:返回值类型不再是StringBuffer本身


StringBuffer和String的相互转换

 A:String -- StringBuffer
	* a:通过构造方法
	* b:通过append()方法
 B:StringBuffer -- String
	* a:通过构造方法
	* b:通过toString()方法
	* c:通过subString(0,length);


StringBuffer和StringBuilder的区别

StringBuilder 
	一个可变的字符序列。
	此类提供一个与 StringBuffer 兼容的 API,但不保证同步。
	该类被设计用作 StringBuffer 的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍)。
	如果可能,建议优先采用该类,因为在大多数实现中,它比 StringBuffer 要快。
StringBuffer和StringBuilder的方法都一样
	* StringBuffer是jdk1.0版本的,是线程安全的,效率低
	* StringBuilder是jdk1.5版本的,是线程不安全的,效率高
String,StringBuffer,StringBuilder的区别
	* String是一个不可变的字符序列
	* StringBuffer,StringBuilder是可变的字符序列


String和StringBuffer分别作为参数传递

	基本数据类型的值传递,不改变其值
	引用数据类型的值传递,改变其值
	String类虽然是引用数据类型,但是他当作参数传递时和基本数据类型是一样的
public static void main(String[] args) {
		String s = "yellow";
		System.out.println(s);
		change(s);//方法一弹栈,方法里面的s就会消失yellowjava就变成垃圾
		System.out.println(s);		
		System.out.println("---------------------");
		StringBuffer sb = new StringBuffer();
		sb.append("yellow");
		System.out.println(sb);
		change(sb);
		System.out.println(sb);
	}
	public static void change(StringBuffer sb) {
		sb.append("java");
	}
	public static void change(String s) {
		s += "java";
	}


public class sbtest {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		StringBuffer sb0 = new StringBuffer();
		System.out.println(sb0);//空
		//StringBuffer类中重写了toString方法,显示的是对象中的属性值
		
		
		/*StringBuffer sb1 = new StringBuffer();
		StringBuffer sb2 = sb1.append(true);
		StringBuffer sb3 = sb2.append("whale");
		StringBuffer sb4 = sb3.append(100);
		
		System.out.println(sb1.toString());
		System.out.println(sb2.toString());
		System.out.println(sb3.toString());
		System.out.println(sb4.toString());//都为truewhale100	
		
		StringBuffer sb1 = new StringBuffer();
		System.out.println(sb1.toString());
		StringBuffer sb2 = sb1.append(true);
		System.out.println(sb2);
		StringBuffer sb3 = sb2.append("whale");
		System.out.println(sb3);
		StringBuffer sb4 = sb3.append(100);
		System.out.println(sb4.toString());
		/*true
		truewhale
		truewhale100*/
		
		StringBuffer sb = new StringBuffer("1234");
		sb.insert(3, "dog");//在指定位置添加元素,如果没有指定位置的索引就会报索引越界异常
		//java.lang.StringIndexOutOfBoundsException
		System.out.println(sb);//123dog4
		
		StringBuffer sb_del = new StringBuffer("bluewhale");
		sb_del.deleteCharAt(2);根据索引删除掉索引位置上对应的字符
		System.out.println(sb_del);//blewhale
		sb_del.delete(0, 3);//
		System.out.println(sb_del);//whale删除的时候是包含头,不包含尾
		StringBuffer s_del = new StringBuffer("blue");
		s_del.delete(0, s_del.length());清空缓冲区
		System.out.println("s_del = "+s_del);
		System.out.println("s_del = "+ s_del.capacity());
		sb_del = new StringBuffer();//不要用这种方式清空缓冲区,原来的会变成垃圾,浪费内存
		System.out.println(sb_del);
		
		StringBuffer s_replace = new StringBuffer("helloworld");
		s_replace.replace(0, 5, "你好");// 不包含尾
		System.out.println(s_replace);//你好world
		
		s_replace.reverse();
		System.out.println(s_replace);//dlrow好你
		
		StringBuffer sj = new StringBuffer("seasons");
		sj.substring(2); //0 1 2 a
		System.out.println("sj = " + sj);//sj = seasons
		String sj0 = sj.substring(2); 
		System.out.println("sj0 = " + sj0);//sj0 = asons
		
		StringBuffer s_tr = new StringBuffer("pigwoman");
		String s1 = new String(s_tr);//通过构造将StringBuffer转换为String
		System.out.println(s1);//pigwoman
		String s2 = s_tr.toString();//通过toString方法将StringBuffer转换为String
		System.out.println(s2);
		String s3 = s_tr.substring(0, s_tr.length());//通过截取子字符串将StringBuffer转换为String
		System.out.println(s3);
		
		StringBuffer sb_tr = new StringBuffer("peoplepig");//通过构造方法将字符串转换为StringBuffer对象
		System.out.println(sb_tr);//peoplepig
		StringBuffer sb_trap = new StringBuffer();
		sb_trap.append("pig");//通过append方法将字符串转换为StringBuffer对象
		System.out.println(sb_trap);//pig
	}

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值