StringBuffer-字符串缓冲区

来源:互联网 发布:暗物质暗能量 知乎 编辑:程序博客网 时间:2024/05/16 11:23

StringBuffer是字符串缓冲区(是一个容器)

特点:

  • 1,长度是可变化的。
  • 2,可以字节操作多个数据类型。
  • 3,最终会通过toString方法变成字符串。

1、存储

  • StringBuffer append();将制定数据作为参数添加到已有数据结尾处
  • StringBuffer insert(index,数据):可以将数据插入到指定index位置

2、删除

  • StringBuffer delete(start,end):删除缓冲区中的数据,包含start,不包含end
  • StringBuffer deleteCharAt(index):删除制定位置的字符

3、获取

  • char charAt(int index);获取指定索引位置的字符
  • int indexOf(String str);返回第一次出现的指定子字符串在该字符串中的索引
  • int lastIndexOf(String str):从尾巴开始
  • int length();
  • String substring(int start,int end);

4、修改

  • StringBuffer replace(start,end,string);
  • void setCharAt(int index,char ch);将给定索引处的字符设置为 ch。

5、反转

  • StringBuffer reverse();将此字符序列用其反转形式取代。

6、将缓冲区中指定数据存储到指定字符数组中

  • void getChars(int srcBegin,int serEnd,char[] dst,int dstBegin);将字符从此序列复制到目标字符数组 dst

JDK1.5版本之后出现了StringBuilder

  • StringBuffer是线程同步
  • StringBuilder是线程不同步

以后开发,建议使用StringBuiler

升级三个因素:

  • 1、提高效率
  • 2、简化书写
  • 3、提高安全性

代码示例:

public class StringBufferDemo01 {    public static void main(String[] args) {//        method_1();//        method_2();//        method_update();//        method_delete();//        method_add();        draw(3,3);    }    public static void draw(int row,int col){        StringBuilder sb = new StringBuilder();        for(int x=0;x<row;x++){            for(int y=0;y<col;y++){                sb.append("*");            }            sb.append("\r\n");        }        System.out.println(sb.toString());    }    public static void method_add(){        StringBuffer sb = new StringBuffer();        StringBuffer sb1 = new StringBuffer(123);        sb.append("abc").append(true).append(123);        System.out.println("sb==sb1:"+(sb==sb1));        sb.insert(1,"qq");//在索引为1的位置插入        System.out.println(sb.toString());    }    public static void method_delete(){        StringBuffer sb = new StringBuffer("abcdefg");        System.out.println("删除前:"+sb.toString());        sb.delete(1, 3);//删除,包含1不包含3        System.out.println("删除后:"+sb.toString());        StringBuilder sb1 = new StringBuilder("123456789");        sb1.deleteCharAt(5);//删除指定位置的字符        System.out.println("删除指定位置后:"+sb1.toString());        sb1.delete(0, sb1.length());//清空缓冲区        System.out.println("清空缓冲区:"+sb1);    }    public static void method_update(){        StringBuffer sb = new StringBuffer("abcdefg");        sb.replace(1, 4, "java");//替换        System.out.println(sb.toString());        sb.setCharAt(2, 'k');//设置        System.out.println(sb.toString());    }    public static void method_2(){        StringBuilder sb = new StringBuilder("abcdefgh");        char[] chs = new char[6];        //将字符串中的从索引1到4(不包含4)的字符复制到字符数组中,字符数组从索引1开始复制        sb.getChars(1, 4, chs, 1);        for(int x=0;x<chs.length;x++){            System.out.println("chs["+x+"]="+chs[x]+";");        }    }    public static void method_1(){        StringBuilder sb = new StringBuilder();        sb.append(new StringDemo01()).append("new StringDemo01()");        sb.append("new StringDemo02()");        System.out.println(sb.toString());    }}
0 0
原创粉丝点击