java学习笔记之StringBuffer(一)

来源:互联网 发布:重低音增强软件 编辑:程序博客网 时间:2024/05/19 18:09
    // StringBuffer();    StringBuffer buffer = new StringBuffer();    System.out.print(buffer + " 0\t");    // StringBuffer(CharSequence seq)    buffer = new StringBuffer("abc");    System.out.print(buffer + " 1\t");    // StringBuffer(int capacity)    // 感谢:http://blog.csdn.net/zuoanren/article/details/8612304    buffer = new StringBuffer(1);    System.out.print(buffer + " 2\t");    // StringBuffer(String str)    buffer = new StringBuffer("abc");    System.out.print(buffer + " 3\t");

结果: 0  abc 1   2  abc 3

    // append(boolean b)    // append(char c)    // append(char[] str)    // append(CharSequence s)    // append(double d)    // append(float f)    // append(int i)    // append(long lng)    // append(Object obj)    // append(String str)    // append(StringBuffer sb)    // 以上都用与添加    StringBuffer buffer = new StringBuffer();    // append(boolean b)    buffer = buffer.append(true);    System.out.println(buffer + "\t");    // append(char c)    buffer = buffer.append('A');    System.out.println(buffer + "\t");    // append(char[] str)    buffer = buffer.append(new char[] { 'a', 'b' });    System.out.println(buffer + "\t");    // append(CharSequence s)    buffer = buffer.append("B");    System.out.println(buffer + "\t");    // append(double d)    buffer = buffer.append(0.00);    System.out.println(buffer + "\t");    // append(float f)    buffer = buffer.append(0.00f);    System.out.println(buffer + "\t");    // append(int i)    buffer = buffer.append(16);    System.out.println(buffer + "\t");    // append(long lng)    buffer = buffer.append(16l);    System.out.println(buffer + "\t");    // append(Object obj)    buffer = buffer.append(new Object());    System.out.println(buffer + "\t");    // append(String str)    buffer = buffer.append(new String());    System.out.println(buffer + "\t");    // append(StringBuffer sb)    buffer = buffer.append(new StringBuffer());    System.out.println(buffer + "\t");

结果:
true
trueA
trueAab
trueAabB
trueAabB0.0
trueAabB0.00.0
trueAabB0.00.016
trueAabB0.00.01616
trueAabB0.00.01616java.lang.Object@659e0bfd
trueAabB0.00.01616java.lang.Object@659e0bfd
trueAabB0.00.01616java.lang.Object@659e0bfd

    // append(char[] str, int offset, int len)    // append(CharSequence s, int start, int end)    // 需要注意的是len 和 end代表的意思不一样,一个代表长度    // 一个代表结束位置    StringBuffer buffer = new StringBuffer();    char[] str = { 'a', 'b', 'c', 'd' };    String s = "ABCD";    // append(char[] str, int offset, int len)    buffer = buffer.append(str,1,3);    System.out.println(buffer + "\t");    // append(CharSequence s, int start, int end)    buffer = buffer.append(s,1,3);    System.out.println(buffer + "\t");

结果:
bcd
bcdBC

// appendCodePoint(int codePoint)    // codePoint ascii 编码    StringBuffer buffer = new StringBuffer();    buffer = buffer.appendCodePoint(97);    System.out.println(buffer + "\t");

结果 : a

capacity() api 解释 Returns the current capacity.

下面这些和string的方法作用一样

    StringBuffer buffer = new StringBuffer("abcdefg");    // charAt(int index)    // 返回指定index 位置的字符,    char charAt = buffer.charAt(5);    System.out.println(charAt + "\t");    // codePointAt(int index)    // 返回指定index 位置的字符的Unicode 编码    int codePointAt = buffer.codePointAt(0);    System.out.println(codePointAt + "\t");    // codePointBefore(int index)    // 返回指定index-1 位置的字符的Unicode 编码    int codePointBefore = buffer.codePointBefore(1);    System.out.println(codePointBefore + "\t");    // codePointCount(int beginIndex, int endIndex)    // 返回endIndex - beginIndex 之间有多少个Unicode    // 编码,endIndex>=beginIndex(必须,不然报错)    // str.codePointCount(0, str.length()) == str.length()    int codePointCount = buffer.codePointCount(0, buffer.length());    System.out.println(codePointCount + "\t");

结果: f   97  97  7

0 0
原创粉丝点击