StringBuffer
定义:线程安全的可变字符序列——字符串可以改变
String: 定义后,内容和长度就固定了 是一个长度不可变得字符序列
往容器中增加,删除,替换,反转内容 返回得还是容器本身
public StringBuffer(): 无参构造方法——初始容量默认16字符
eg: StringBuffer sb1
= new
StringBuffer();
public StringBuffer(int capacity): 指定容量的字符串缓冲区对象
eg: StringBuffer sb2
= new
StringBuffer(50);
public StringBuffer(String str): 指定字符串内容的字符串缓冲区对象
eg: StringBuffer sb3
= new
StringBuffer("没有秘密的你");
StringBuffer的方法
public int capacity( ):返回当前容量——理论值
eg: System
.out
.println(sb3
.capacity());
public int length( ):返回长度(字符数)——实际值
eg: System
.out
.println(sb
.length());
public StringBuffer append(String str): 可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
eg: StringBuffer sb
= new
StringBuffer();
StringBuffer ap1
= sb
.append("没有");
System
.out
.println(ap1
);———————————— 输出:没有
StringBuffer ap2
= ap1
.append("秘密");
System
.out
.println(ap2
);————————————输出:没有秘密
System
.out
.println(sb1
.append("的你"));————————————输出:没有秘密的你
StringBuffer s
= sb
.append(123).append(789).append("江夏").append("没有秘密的你");————————————输出:
123789江夏没有秘密的你
public StringBuffer insert(int offset,String str):在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
eg
: StringBuffer sb
= new
StringBuffer();
sb
.append("亲爱的").append("热爱的");
StringBuffer s
= sb
.insert(3, "韩商言");
System
.out
.println(s
);——————————————输出:亲爱的韩商言热爱的
public StringBuffer deleteCharAt(int index):删除指定位置的字符 并返回本身
eg
: .......
System
.out
.println(sb
.deleteCharAt(9));——————————————输出:亲爱的韩商言热爱
public StringBuffer delete(int start,int end):删除从指定位置开始指定位置结束的内容,并返回本身
eg:
......
System
.out
.println(sb
.delete(6, 8));————————————————输出:亲爱的韩商言
public StringBuffer replace(int start,int end,String str): 从start开始到end用str替换
eg:
......
System
.out
.println(sb
.replace(3, 6,"佟年"));——————————输出:亲爱的佟年
public StringBuffer reverse(): 字符串反转
eg:
......
System
.out
.println(sb
.reverse());——————————————输出:年佟的爱亲
public String substring(int start):从指定位置截取到末尾
eg:
......
System
.out
.println(sb
.reverse());
System
.out
.println(sb
.substring(sb
.indexOf("佟")));——————————输出:佟年
public String substring(int start,int end): 截取从指定位置开始到结束位置,包括开始位置,不包括结束位置
eg
: ......
System
.out
.println(sb
.append("是个天真的小姑娘"));
System
.out
.println(sb
.substring(sb
.indexOf("佟"), sb
.lastIndexOf("娘")+1));
——————————输出:亲爱的佟年是个天真的小姑娘
佟年是个天真的小姑娘
String 和 StringBuffer 的输出
eg: String s
= "我的歌词不限于格式模式不跟着押运走——福克斯";
System
.out
.println(new
StringBuffer(s
));
System
.out
.println(new
StringBuffer().append(s
));
String s1
= new
String("HeyKong Can You Hear Me");
System
.out
.println(new
String(s1
));
System
.out
.println(s1
.toString());
System
.out
.println(s1
.substring(0));
StringBuilder
定义:一个可变的字符序列。用在字符串缓冲区被单个线程使用的时候———比StringBuffer效率高
StringBuilder和String的区别
eg:public
static void main(String
[] args
) {
String s
= "hello";
test(s
);
System
.out
.println(s
);———————————— 输出:hello
StringBuilder sb
= new
StringBuilder(s
);
test(sb
);
System
.out
.println(sb
);———————————— 输出:helloWorld !
}
private
static void test(String s
){
s
= s
+"world" ;
System
.out
.println(s
);———————————— 输出:helloworld
}
private
static void test(StringBuilder s
) {
System
.out
.println(s
);
s
.append("World !");———————————— 输出:helloWorld !
}