• 相关软件
    >StringBuffer 创建者:webmaster 更新时间:2005-12-17 03:24

    字符串缓冲区可以被多个线程安全地使用。 这些方法在需要的时侯可以同步执行,因而任一特定实例的所有操作就好象是以串行的次序发生 。


    字符串缓冲区被编译器用来实现连接两个字符串的运算符
    +。 例如,代码:


      x = "a" + 4 + "c"


    被编译成等价的:


      x = new StringBuffer().append("a").append(4).append("c")
                      .toString()


    StringBuffer 上的基本操作是 append
    insert 方法,它们都被覆盖以接受任意类型的数据。
    每个方法都迅速地把所给的数据转换成一个字符串,并且把该字符串的字符添加或插入
    到字符串缓冲区中。

    append 方法常常把这些字符添加到缓冲区的末端;
    insert 方法则把这些字符添加到指定的地方 。


    例如,如果 z 指的是一个字符串缓冲区内的对象 ,其当前的内容是 "start",于是通过调用方法
    z.append("le") 会使字符串缓冲区的内容变成"startle",而
    z.insert(4, "le") 将使缓冲区的内容变成
    "starlet"。


    每一个字符串缓冲区都有一定的容量。 只要字符串缓冲区存储的字符序列的
    长度没有超过它的容量,就无须申请新的内部缓冲数组 。
    如果内部的缓冲区溢出,它将自动加以扩大。


    类 java.lang.StringBuffer



    java.lang.Object
      |
      +----java.lang.StringBuffer




    public final class StringBuffer
    extends Object
    implements Serializable


    字符串缓冲区实现可变字符序列。

     



    来自:
    JDK1.0
     
    参见:
     
    ByteArrayOutputStream, String





    构造子索引



    StringBuffer()
    构造一个不包含字符的字符串缓冲区,其初始的容量设为 16 个字符。
    StringBuffer(int)
    构造一个不包含字符的字符串缓冲区,其初始容量由参数 length 设定。
    StringBuffer(String)
    构造一个字符串缓冲区,使它表示和字符串参数相同的字符序列。



    方法索引



    append(boolean)
    boolean 型参数的字符串表示添加到字符串缓冲区。
    append(char)
    char 型参数的字符串表示添加到字符串缓冲区。
    append(char[])
    char 型数组参数的字符串表示添加到字符串缓冲区。
    append(char[], int, int)
    char 型数组参数的子数组的字符串表示添加到字符串缓冲区。
    append(double)
    double 型参数的字符串表示添加到该字符串缓冲区。
    append(float)
    float 型参数的字符串表示添加到该字符串缓冲区。
    append(int)
    int 型参数的字符串表示添加到该字符串缓冲区。
    append(long)
    long 型参数的字符串表示添加到该字符串缓冲区。
    append(Object)
    Object 型参数的字符串表示添加到该字符串缓冲区。
    append(String)
    添加字符串到字符串缓冲区。
    capacity()
    返回字符串缓冲区的当前容量。
    charAt(int)
    返回字符串缓冲区中特定位置的字符。
    ensureCapacity(int)
    保证缓冲区的容量至少等于指定的最小数。
    getChars(int, int, char[], int)
    把字符从字符串缓冲区拷贝到目标字符数组
    dst
    insert(int, boolean)
    boolean 型参数的字符串表示添加到该字符串缓冲区。
    insert(int, char)
    char 型参数的字符串表示添加到该字符串缓冲区。
    insert(int, char[])
    char 型数组参数的字符串表示添加到该字符串缓冲区。
    insert(int, double)
    double 型参数的字符串表示添加到该字符串缓冲区。
    insert(int, float)
    float 型参数的字符串表示添加到该字符串缓冲区。
    insert(int, int)
    把第二个 int 型参数表示的字符串插入到该字符串缓冲区。
    insert(int, long)
    long 型参数的字符串表示插入到该字符串缓冲区。
    insert(int, Object)
    Object 型参数的字符串表示插入到该字符串缓冲区。
    insert(int, String)
    把字符串插入到字符串缓冲区。
    length()
    返回字符串缓冲区的长度 (字符数)。
    reverse()
    把字符串缓冲区的字符序列用其反向字符序列替换。
    setCharAt(int, char)
    字符串缓冲区指定位置的字符设置为
    ch
    setLength(int)
    设置字符串缓冲区的长度。
    toString()
    把字符串缓冲区的数据转换为字符串表示。




    构造子




    StringBuffer

    public StringBuffer()


    构造一个不包含字符的字符串缓冲区,其初始的容量设为 16 个字符。




    StringBuffer

    public StringBuffer(int length)


    构造一个不包含字符的字符串缓冲区,其初始的容量由参数 length 设置。



     
    参数:
     
    length - 初始的容量。
     
    抛出:
    NegativeArraySizeException
     
    如果参数 length 小于 0



    StringBuffer

    public StringBuffer(String str)


    构造一个字符串缓冲区,来表示和字符串参数相同的字符序列。 字符串缓冲区的初始容量
    16 加上字符串参数的长度。



     
    参数:
     
    str - 缓冲区的初始内容 。





    方法



    length

    public int length()


    返回字符串缓冲区的长度 (字符数)。



     
    返回:
     
    字符串缓冲区的字符数。



    capacity

    public int capacity()


    返回字符串缓冲区的当前容量。 该容量表示可用于插入新的字符的存储空间;超出该容量时会发生新的容量分配。



     
    返回:
     
    字符串缓冲区的当前容量。



    ensureCapacity

    public synchronized void ensureCapacity(int minimumCapacity)


    保证缓冲区的容量至少等于指定的最小数。
    如果字符串缓冲区的当前容量少于该参数,则分配一个新的更大的内部缓冲区。
    新容量将取如下参数中较大的一个:

    • 参数 minimumCapacity
    • 旧容量的两倍加 2.

    如果参数 minimumCapacity 非正,该方法不作任何操作,只简单地返回。



     
    参数:
     
    minimumCapacity - 要求的最小容量。



    setLength

    public synchronized void setLength(int newLength)


    设置该字符串缓冲区的长度。
    如果参数 newLength 小于该字符串缓冲区的当前长度。
    该字符串缓冲区将被截断来包含恰好等于由参数 newLength 给出的字符数。


    如果参数 newLength 大于或等于当前的长度,就用 null 字符 ('\u0000')补齐该字符串缓冲区,以使长度变为参数 newLength 指定的长度。


    参数 newLength 必须大于或等于 0



     
    参数:
     
    newLength - 缓冲区的新建长度 。  
    抛出:
    StringIndexOutOfBoundsException
     
    如果参数
              newLength 是 非法的。
     
    参见:
     
    length



    charAt

    public synchronized char charAt(int index)


    返回在字符串缓冲区特定位置的字符。


    字符串缓冲区的第一个字符的索引是 0,下一个的索引是
    1,对于索引数组依次类推。


    偏移参量必须大于或等于 0,但是小于字符串缓冲区的长度。



     
    参数:
     
    index - 期望的字符的索引。
     
    返回:
     
    该字符串缓冲区指定位置的字符。
     
    抛出:
    StringIndexOutOfBoundsException
     
    如果索引无效。
     
    参见:
     
    length



    getChars

    public synchronized void getChars(int srcBegin,
                          int srcEnd,
                          char dst[],
                          int dstBegin)


    把字符从该字符串缓冲区拷贝到目标字符数组 dst。   第一个要复制的字符在下标
    srcBegin; 最后一个要复制的字符在下标 srcEnd-1 。 要复制的字符总数是
    srcEnd-srcBegin 。 要复制到 dst 子数组的字符开始于下标 dstBegin,结束于下标


      dstbegin + (srcEnd-srcBegin) - 1



     
    参数:
     
    srcBegin - 在字符串缓冲区开始复制的偏移量 。  
    srcEnd - 在字符串缓冲区停止复制的偏移量 。
     
    dst - 数据将复制到的数组。
     
    dstBegin - dst 的偏移量。
     
    抛出:
    StringIndexOutOfBoundsException
     
    如果有一个对该缓冲区无效的索引 。



    setCharAt

    public synchronized void setCharAt(int index,
                            char ch)


    将该字符串缓冲区指定位置的字符被设置成 ch


    偏移参量必须大于或等于 0,但小于字符串缓冲区的长度。



     
    参数:
     
    index - 要修改的字符的索引。
     
    ch - 新的字符。
     
    抛出:
    StringIndexOutOfBoundsException
     
    如果索引无效。
     
    参见:
     
    length



    append

    public synchronized StringBuffer append(Object obj)


    Object 型参数的字符串表示添加到该字符串缓冲区。


    参数先转换成一个字符串,就好象通过 String.valueOf 方法一样,并且该字符串的字符随后被添加到了字符串缓冲区。



     
    参数:
     
    obj - 一个 Object.
     
    返回:
     
    字符串缓冲区。
     
    参见:
     
    valueOf, append



    append

    public synchronized StringBuffer append(String str)


    添加该字符串到字符串缓冲区。


    把参数 String 的字符依次添加到字符串缓冲区中,字符串缓冲区长度的增加值为该参数的长度。



     
    参数:
     
    str - 一字符串。
     
    返回:
     
    字符串缓冲区。



    append

    public synchronized StringBuffer append(char str[])


    char 型数组参数的字符串表示添加到字符串缓冲区。


    把数组参数的字符依次添加到字符串缓冲区中。
    字符串缓冲区的长度的增加值为参数的长度。



     
    参数:
     
    str - 将添加的字符。
     
    返回:
     
    字符串缓冲区。



    append

    public synchronized StringBuffer append(char str[],
                              int offset,
                              int len)


    char 型数组参数的子数组的字符串表示添加到字符串缓冲区。


    把字符数组 str 从下标 offset 开始的字符,依次添加到了字符串缓冲区的内容中。
    该字符串缓冲区的长度增加了
    len



     
    参数:
     
    str - 将添加的字符。
     
    offset - 要添加的第一个字符的下标。
     
    len - 添加的字符数
     
    返回:
     
    字符串缓冲区。



    append

    public StringBuffer append(boolean b)


    boolean 型参数的字符串表示添加到字符串缓冲区。


    参数先转换成一个字符串,就好象通过 String.valueOf 方法一样,并且该字符串的字符接着也添加到了字符串缓冲区。



     
    参数:
     
    b - 一个 boolean 量。
     
    返回:
     
    字符串缓冲区。
     
    参见:
     
    valueOf, append



    append

    public synchronized StringBuffer append(char c)


    char 型参数的字符串表示添加到字符串缓冲区。


    参数添加到字符串缓冲区的内容中。
    字符串缓冲区的长度增加了 1



     
    参数:
     
    ch - 一个char
     
    返回:
     
    字符串缓冲区。



    append

    public StringBuffer append(int i)


    int 型参数的字符串表示添加到该字符串缓冲区。


    参数先转换成一个字符串,就好象通过 String.valueOf 方法一样,并且该字符串的字符随后被添加到了字符串缓冲区。



     
    参数:
     
    i - 一个 int.
     
    返回:
     
    字符串缓冲区。
     
    参见:
     
    valueOf, append



    append

    public StringBuffer append(long l)


    long 型参数的字符串表示添加到该字符串缓冲区。


    参数先转换成一个字符串,就好象通过 String.valueOf 方法一样,并且该字符串的字符随后被添加到了字符串缓冲区。



     
    参数:
     
    l - 一个 long.
     
    返回:
     
    字符串缓冲区。
     
    参见:
     
    valueOf, append



    append

    public StringBuffer append(float f)


    float 型参数的字符串表示添加到该字符串缓冲区。


    参数先转换成一个字符串,就好象通过 String.valueOf 方法一样,并且该字符串的字符随后被添加到了字符串缓冲区。



     
    参数:
     
    f - 一个 float.
     
    返回:
     
    字符串缓冲区。
     
    参见:
     
    valueOf, append



    append

    public StringBuffer append(double d)


    double 型参数的字符串表示添加到该字符串缓冲区。


    参数先转换成一个字符串,就好象通过 String.valueOf 方法一样,并且该字符串的字符随后被添加到了字符串缓冲区。



     
    参数:
     
    d - 一个 double.
     
    返回:
     
    字符串缓冲区。
     
    参见:
     
    valueOf, append



    insert

    public synchronized StringBuffer insert(int offset,
                              Object obj)


    Object 型参数的字符串表示插入到字符串缓冲区。


    第二个参数先转换成一个字符串,就好象通过 String.valueOf 方法一样,并且该字符串的字符随后被插入到字符串缓冲区的指定偏移位置。


    偏移参量必须大于或等于 0,小于或等于字符串缓冲区的长度。



     
    参数:
     
    offset - 偏移量。
     
    b - 一个 Object.
     
    返回:
     
    字符串缓冲区。
     
    抛出:
    StringIndexOutOfBoundsException
     
    如果偏移无效。  
    参见:
     
    valueOf, insert, length



    insert

    public synchronized StringBuffer insert(int offset,
                              String str)


    把该字符串插入到该字符串缓冲区。


    参数 String 的字符依次插入到字符串缓冲区的指定偏移位置。
    字符串缓冲区的长度的增加值为参数的长度。


    偏移参量必须大于或等于 0,小于或等于字符串缓冲区的长度。



     
    参数:
     
    offset - 偏移量。
     
    str - 一个字符串。
     
    返回:
     
    字符串缓冲区。
     
    抛出:
    StringIndexOutOfBoundsException
     
    如果偏移无效。  
    参见:
     
    length



    insert

    public synchronized StringBuffer insert(int offset,
                              char str[])


    char 型数组参数的字符串表示添加到该字符串缓冲区。


    把数组参数的字符添加到字符串缓冲区里由 offset 指定的位置。 字符串缓冲区长度的增加值为参数的长度。



     
    参数:
     
    offset - 偏移量。
     
    ch - 一个字符数组。
     
    返回:
     
    字符串缓冲区。
     
    抛出:
    StringIndexOutOfBoundsException
     
    如果偏移无效。



    insert

    public StringBuffer insert(int offset,
                      boolean b)


    boolean 型参数的字符串表示添加到该字符串缓冲区。


    第二个参数先转换成一个字符串,就好象通过 String.valueOf 方法一样,并且该字符串的字符被添加到了字符串缓冲区指定的偏移位置。


    偏移参量必须大于或等于 0,小于或等于字符串缓冲区的长度。



     
    参数:
     
    offset - 偏移量。
     
    b - 一个 boolean 量。
     
    返回:
     
    字符串缓冲区。
     
    抛出:
    StringIndexOutOfBoundsException
     
    如果偏移无效。
     
    参见:
     
    valueOf, insert, length



    insert

    public synchronized StringBuffer insert(int offset,
                              char c)


    char 型参数的字符串表示添加到该字符串缓冲区。


    把第二个参数表示的字符插入到字符串缓冲区里由 offset 指定的位置。 字符串缓冲区的长度增加了 1 。


    偏移参量必须大于或等于
    0,小于或等于字符串缓冲区的长度。



     
    参数:
     
    offset - 偏移量。
     
    ch - 一个char
     
    返回:
     
    字符串缓冲区。
     
    抛出:
    StringIndexOutOfBoundsException
     
    如果偏移无效。
     
    参见:
     
    length



    insert

    public StringBuffer insert(int offset,
                      int i)


    把第二个 int 型参数的字符串表示插入到该字符串缓冲区。


    第二个参数先转换成一个字符串,就好象通过 String.valueOf 方法一样,并且该字符串的字符随后被添加到了字符串缓冲区指定的偏移位置。


    偏移参量必须大于或等于 0,小于或等于字符串缓冲区的长度。



     
    参数:
     
    offset - 偏移量。
     
    b - 一个 int.
     
    返回:
     
    字符串缓冲区。
     
    抛出:
    StringIndexOutOfBoundsException
     
    如果偏移无效。
     
    参见:
     
    valueOf, insert, length



    insert

    public StringBuffer insert(int offset,
                      long l)


    long 型参数的字符串表示插入到该字符串缓冲区。


    第二个参数先转换成一个字符串,就好象通过 String.valueOf 方法一样,并且该字符串的字符随后被添加到了字符串缓冲区指定的偏移位置。


    偏移参量必须大于或等于 0,小于或等于字符串缓冲区的长度。



     
    参数:
     
    offset - 偏移量。
     
    b - 一个 long.
     
    返回:
     
    字符串缓冲区。
     
    抛出:
    StringIndexOutOfBoundsException
     
    如果偏移无效。
     
    参见:
     
    valueOf, insert, length



    insert

    public StringBuffer insert(int offset,
                      float f)


    把参数 float 的字符串表示添加到该字符串缓冲区。


    第二个参数先转换成一个字符串,就好象通过 String.valueOf 方法一样,并且该字符串的字符随后被添加到了字符串缓冲区指定的偏移位置。


    偏移参量必须大于或等于 0,小于或等于字符串缓冲区的长度。



     
    参数:
     
    offset - 偏移量。
     
    b - 一个 float.
     
    返回:
     
    字符串缓冲区。
     
    抛出:
    StringIndexOutOfBoundsException
     
    如果偏移无效。
     
    参见:
     
    valueOf, insert, length



    insert

    public StringBuffer insert(int offset,
                      double d)


    把参数 double 的字符串表示添加到该字符串缓冲区。


    第二个参数先转换成一个字符串,就好象通过 String.valueOf 方法一样,并且该字符串的字符随后被添加到了字符串缓冲区指定的偏移位置。


    偏移参量必须大于或等于 0,小于或等于字符串缓冲区的长度。



     
    参数:
     
    offset - 偏移量。
     
    b - 一个 double.
     
    返回:
     
    字符串缓冲区。
     
    抛出:
    StringIndexOutOfBoundsException
     
    如果偏移无效。
     
    参见:
     
    valueOf, insert, length



    reverse

    public synchronized StringBuffer reverse()


    该字符串缓冲区的字符序列被其反向字符序列所替换。



     
    返回:
     
    字符串缓冲区。



    toString

    public String toString()


    转换为一个表示该字符串缓冲区数据的字符串。
    分配一个新的 String 对象,并且用字符串缓冲区所表示的字符序列进行初始化。
    于是此 String 被返回。 随后缓冲区发生的变化不再影响该 String 的内容。



     
    返回:
     
    字符串缓冲区的字符串表示法。
     
    覆盖:
     
    类 Object 中的
    toString

    相关文章
    本页查看次数: