/*说来惭愧,距离上次更博已经过去好长时间了,因为项目,因为实训。话不多说,我又回来啦,哈哈,感觉真好!*/
认识StringBuffer类
前面说过String类的一条特性是,String的内容一旦声明则不可更改,
如果改变,则改变的是String的引用地址,那么如果现在的情况是一个字符串经常需要改变,此时就必须使用StringBuffer类。
String类中可以通过“+”进行字符串的连接,但在StringBuffer中却只能使用append()方法进行字符串的连接。
下面介绍StringBuffer的常用方法:
- 字符串连接操作
在程序中使用append()方法可以进行进行字符串的连接,而且此方法返回了一个StringBuffer类的实例,这样就可以采用代码链的形式进行添加数据。
通过append()添加数据
public class StringBufferDemo01
{
public static void main(String args[]){
StringBuffer buf=new StringBuffer();
buf.append("hello");
buf.append("wordld").append("!!");
buf.append("\n");
buf.append("数字=").append(12).append("\n");
buf.append("字符=").append('C').append("\n");
buf.append("布尔=").append(true);
System.out.println(buf);
}
}
验证StringBuffer的内容是可以修改的
public class StringBufferDemo02
{
public static void main(String args[]){
StringBuffer buf=new StringBuffer();
buf.append("Hello");
fun(buf);
System.out.println(buf);
}
public static void fun(StringBuffer s){
s.append("\taotu");
}
}
从运行的结果来看,将StringBuffer对象的内容传递到了fun()方法后,对StringBuffer的内容进行了修改,而且操作完毕后修改的内容被保存下来,所以与String比较StringBuffer的内容是可以进行修改的。
- 在任意位置处为StringBuffer添加内容
可以直接使用insert()方法在指定的位置上为StringBuffer添加内容,
public class StringBufferDemo03
{
public static void main(String args[]){
StringBuffer buf=new StringBuffer();
buf.append("World!!");
buf.insert(0,"Hello");
System.out.println(buf);
}
}
- 字符串反转操作
在StringBuffer中专门提供了字符串反转的操作方法:
public class StringBufferDemo04
{
public static void main(String args[]){
StringBuffer buf=new StringBuffer();
buf.append("hello");
buf.insert(0,"zhao");
System.out.println(buf);
System.out.println(buf.reverse().toString());
}
}
字符串反转实际是通过入栈及出栈功能完成的,关于栈(Stack)在java类集中将为读者介绍。
- 替换指定范围的内容
在StringBuffer类中国也存在replace()方法,使用此方法可以对指定范围的内容进行替换。
public class StringBufferDemo05
{
public static void main(String args[]){
StringBuffer buf=new StringBuffer();
buf.append("hello").append("world!!");
buf.replace(6,11,"Zhao");
System.out.println(buf);
}
}
在String中如果进行替换,则使用的是replaceAll()方法,而在StringBuffer中使用的是replace()方法,这一点要注意。
- 字符串截取
通过substring()方法可以直接从StringBuffer的指定范围中截取内容。
public class StringBufferDemo06
{
public static void main(String[] args){
StringBuffer buf=new StringBuffer();
buf.append("hello").append("world");
buf.replace(6,11,"zhao");
String str=buf.substring(6,9);
System.out.println(str);
}
}
- 删除指定范围的字符串
因为StringBuffer本身的内容是可以更改的,所以也可以通过delete()方法删除指定范围的内容。
public class StringBufferDemo07
{
public static void main(String[] args){
StringBuffer buf=new StringBuffer();
buf.append("hello").append("world");
String str=buf.delete(5,11).toString();
System.out.println(str);
}
}
- 查找指定的内容是否存在
通过indexOf()方法可以查找指定的内容,如果找到了,则返回内容的位置,如果没有查找到则返回-1
public class StringBufferDemo08
{
public static void main(String[] args){
StringBuffer buf=new StringBuffer();
buf.append("hello").append("world");
if (buf.indexOf("hello")==-1)
{
System.out.println("没有查找到内容!");
}else {
System.out.println("查找到了!");
}
}
}
从上面的实例中可以看出,StringBuffer类中的方法的使用和String类相似。
StringBuffer类的应用
在使用String类时,有以下代码:
public class StringBufferDemo09
{
public static void main(String args[]){
String str1="Zhao";
for (int i=0; i<10;i++ )
{
str1 += i;
}
System.out.println(str1);
}
}
上面的代码虽然最后字符串的结果改变了,但实际上是通过不断修改对象的引用来实现的,所以性能很差,也就是说要想解决此类问题就必须靠StringBuffer,因为StringBuffer的内容是可以修改的,代码修改如下:
public class StringBufferDemo09
{
public static void main(String args[]){
StringBuffer str1=new StringBuffer();
str1.append("zhao");
for (int i=0; i<10;i++ )
{
str1.append(i);
}
System.out.println(str1);
}
}
以上程序实现的功能用String也可以实现,但是由于使用了StringBuffer,所以代码的性能有了很大的提高,也就是说对于频繁修改字符串内容的地方,最好使用StringBuffer类完成。