-----------android培训、java培训、java学习型技术博客、期待与您交流!------------
第三讲 StringBuffer类
一、StringBuffer类概述
StringBuffer类他的使用方法跟String类差不了多少,我们这里只讲StringBuffer类,StringBuilder类我们下节再说吧,StringBuffer是线程安全的可变字符序列。一个类似于String 的字符串缓冲区,但不能修改。虽然在任意时间点上它都包含某种特定的字符序列,但通过某些方法调用可以改变该序列的长度和内容。可将字符串缓冲区安全地用于多个线程。可以在必要时对这些方法进行同步,因此任意特定实例上的所有操作就好像是以串行顺序发生的,该顺序与所涉及的每个线程进行的方法调用顺序一致。
简单的说就是线程安全,不过就是效率要比StringBuilder低。
二、StringBuffer类的构造方法
package cn.itcast.twoday;
/*
* A:StringBuffer的构造方法:
public StringBuffer(): 无参构造方法
public StringBuffer(int capacity): 指定容量的字符串缓冲区对象
public StringBuffer(String str): 指定字符串内容的字符串缓冲区对象
B:StringBuffer的方法:
public int capacity():返回当前容量。 理论值
public int length():返回长度(字符数)。 实际值
注意:StringBuffer有toString方法
*/
public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();//创建StringBuffer对象
System.out.println("sb.capacity()容量为:"+sb.capacity());//返回当前容量
System.out.println("sb.length()长度为:"+sb.length());//返回长度
System.out.println("-----------------------------");
StringBuffer sb1 = new StringBuffer(5);//创建StringBuffer对象,指定容量的字符串缓冲区为5
System.out.println("sb1.capacity()容量为:"+sb1.capacity());//返回当前容量
System.out.println("sb1.length()长度为:"+sb1.length());//返回长度
System.out.println("-----------------------------");
StringBuffer sb2 = new StringBuffer("abc");//创建StringBuffer对象,指定容量的字符串缓冲区
System.out.println("sb2.capacity()容量为:"+sb2.capacity());//返回当前容量
System.out.println("sb2.length()长度为:"+sb2.length());//返回长度
}
}
结果为:
sb.capacity()容量为:16
sb.length()长度为:0
-----------------------------
sb1.capacity()容量为:5
sb1.length()长度为:0
-----------------------------
sb2.capacity()容量为:19
sb2.length()长度为:3
三、StringBuffer的功能
1、StringBuffer的添加功能
package cn.itcast.twoday;
/*
* StringBuffer的添加功能
public StringBuffer append(String str):
可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
public StringBuffer insert(int offset,String str):
在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
*/
public class StringBufferDemo2 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();//创建对象
System.out.println(sb.append("hello"));//添加到字符串缓冲区里面,并返回字符串缓冲区本身
StringBuffer sb1 = new StringBuffer("world");//创建对象
System.out.println(sb1.insert(3, "woaini"));
}
}
结果为:
hello
worwoainild
2、StringBuffer的删除功能
package cn.itcast.twoday;
/*
* StringBuffer的删除功能
public StringBuffer deleteCharAt(int index): 删除指定位置的字符,并返回本身
public StringBuffer delete(int start,int end): 删除从指定位置开始指定位置结束的内容,并返回本身
*/
public class StringBufferDemo3 {
public static void main(String[] args) {
//创建对象
StringBuffer sb = new StringBuffer("helloworld");
System.out.println(sb);
System.out.println("------------------");
System.out.println(sb.deleteCharAt(5));//删除指定位置的字符,并返回本身
System.out.println("------------------");
System.out.println(sb.delete(3, 5));
}
}
结果为:
helloworld
------------------
helloorld
------------------
helorld
3、StringBuffer的替换功能
package cn.itcast.twoday;
/*
*StringBuffer的替换功能
public StringBuffer replace(int start,int end,String str): 从start开始到end用str替换
*/
public class StringBufferDemo4 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("helloworld");//创建对象
System.out.println(sb.replace(0, 5, "woaini"));//从start开始到end用str替换
}
}
woainiworld
4、StringBuffer的反转功能
package cn.itcast.twoday;
/*
* StringBuffer的反转功能
public StringBuffer reverse(): 字符串反转
*/
public class StringBufferDemo5 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("helloworld");//创建对象
System.out.println(sb);
System.out.println("------------------------");
System.out.println(sb.reverse());//字符串反转
}
}
helloworld
------------------------
dlrowolleh
5、StringBuffer的截取功能
package cn.itcast.twoday;
/*
* StringBuffer的截取功能
public String substring(int start): 从指定位置截取到末尾
public String substring(int start,int end): 截取从指定位置开始到结束位置,包括开始位置,不包括结束位置
*/
public class StringBufferDemo6 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("helloworld");//创建对象
System.out.println(sb);
System.out.println("------------------------");
System.out.println(sb.substring(5));//从指定位置截取到末尾
System.out.println("------------------------");
System.out.println(sb.substring(3, 8));
}
}
helloworld
------------------------
world
------------------------
lowor
-----------android培训、java培训、java学习型技术博客、期待与您交流!------------