Intel

Developer Zone

StringBuffer

java.lang

Class StringBuffer

  • Direct Known Subclasses:
    StringBuilder


    public class StringBuffer
    extends Object
    StringBuffer is a variable size contiguous indexable array of characters. The length of the StringBuffer is the number of characters it contains. The capacity of the StringBuffer is the number of characters it can hold.

    Characters may be inserted at any position up to the length of the StringBuffer, increasing the length of the StringBuffer. Characters at any position in the StringBuffer may be replaced, which does not affect the StringBuffer length.

    The capacity of a StringBuffer may be specified when the StringBuffer is created. If the capacity of the StringBuffer is exceeded, the capacity is increased.

    Since:
    1.0
    See Also:
    String, StringBuilder
    • Constructor Summary

      Constructors
      Constructor and Description
      StringBuffer()
      Constructs a new StringBuffer using the default capacity which is 16.
      StringBuffer(int capacity)
      Constructs a new StringBuffer using the specified capacity.
      StringBuffer(String string)
      Constructs a new StringBuffer containing the characters in the specified string.
    • Method Summary

      Methods
      Modifier and Type Method and Description
      StringBuffer append(boolean b)
      Adds the string representation of the specified boolean to the end of this StringBuffer.
      StringBuffer append(char ch)
      Adds the specified character to the end of this buffer.
      StringBuffer append(char[] chars)
      Adds the character array to the end of this buffer.
      StringBuffer append(char[] chars, int offset, int length)
      Adds the specified sequence of characters to the end of this buffer.
      StringBuffer append(int i)
      Adds the string representation of the specified integer to the end of this StringBuffer.
      StringBuffer append(long l)
      Adds the string representation of the specified long to the end of this StringBuffer.
      StringBuffer append(Object obj)
      Adds the string representation of the specified object to the end of this StringBuffer.
      StringBuffer append(String string)
      Adds the specified string to the end of this buffer.
      int capacity()
      Returns the number of characters that can be held without growing.
      char charAt(int index)
      Retrieves the character at the index.
      StringBuffer delete(int start, int end)
      Deletes a range of characters.
      StringBuffer deleteCharAt(int location)
      Deletes the character at the specified offset.
      void ensureCapacity(int min)
      Ensures that this object has a minimum capacity available before requiring the internal buffer to be enlarged.
      void getChars(int start, int end, char[] dest, int destStart)
      Copies the requested sequence of characters to the char[] passed starting at destStart.
      StringBuffer insert(int index, boolean b)
      Inserts the string representation of the specified boolean into this buffer at the specified offset.
      StringBuffer insert(int index, char ch)
      Inserts the character into this buffer at the specified offset.
      StringBuffer insert(int index, char[] chars)
      Inserts the character array into this buffer at the specified offset.
      StringBuffer insert(int index, int i)
      Inserts the string representation of the specified integer into this buffer at the specified offset.
      StringBuffer insert(int index, long l)
      Inserts the string representation of the specified long into this buffer at the specified offset.
      StringBuffer insert(int index, Object obj)
      Inserts the string representation of the specified object into this buffer at the specified offset.
      StringBuffer insert(int index, String string)
      Inserts the string into this buffer at the specified offset.
      int length()
      The current length.
      StringBuffer reverse()
      Reverses the order of characters in this buffer.
      void setCharAt(int index, char ch)
      Sets the character at the index.
      void setLength(int length)
      Sets the current length to a new value.
      String substring(int start)
      Returns the String value of the subsequence from the start index to the current end.
      String substring(int start, int end)
      Returns the String value of the subsequence from the start index to the end index.
      String toString()
      Returns the current String representation.
    • Constructor Detail

      • StringBuffer

        public StringBuffer()
        Constructs a new StringBuffer using the default capacity which is 16.
      • StringBuffer

        public StringBuffer(int capacity)
        Constructs a new StringBuffer using the specified capacity.
        Parameters:
        capacity - the initial capacity.
      • StringBuffer

        public StringBuffer(String string)
        Constructs a new StringBuffer containing the characters in the specified string. The capacity of the new buffer will be the length of the String plus the default capacity.
        Parameters:
        string - the string content with which to initialize the new instance.
        Throws:
        NullPointerException - if string is null.
    • Method Detail

      • append

        public StringBuffer append(char[] chars)
        Adds the character array to the end of this buffer.
        Parameters:
        chars - the character array to append.
        Returns:
        this StringBuffer.
        Throws:
        NullPointerException - if chars is null.
      • append

        public StringBuffer append(char[] chars,
                          int offset,
                          int length)
        Adds the specified sequence of characters to the end of this buffer.
        Parameters:
        chars - the character array to append.
        start - the starting offset.
        length - the number of characters.
        Returns:
        this StringBuffer.
        Throws:
        ArrayIndexOutOfBoundsException - if length < 0 , start < 0 or start + length > chars.length.
        NullPointerException - if chars is null.
      • append

        public StringBuffer append(String string)
        Adds the specified string to the end of this buffer.

        If the specified string is null the string "null" is appended, otherwise the contents of the specified string is appended.

        Parameters:
        string - the string to append (may be null).
        Returns:
        this StringBuffer.
      • append

        public StringBuffer append(int i)
        Adds the string representation of the specified integer to the end of this StringBuffer.
        Parameters:
        i - the integer to append.
        Returns:
        this StringBuffer.
        See Also:
        String.valueOf(int)
      • append

        public StringBuffer append(long l)
        Adds the string representation of the specified long to the end of this StringBuffer.
        Parameters:
        l - the long to append.
        Returns:
        this StringBuffer.
        See Also:
        String.valueOf(long)
      • append

        public StringBuffer append(Object obj)
        Adds the string representation of the specified object to the end of this StringBuffer.

        If the specified object is null the string "null" is appended, otherwise the objects toString is used to get its string representation.

        Parameters:
        obj - the object to append (may be null).
        Returns:
        this StringBuffer.
        See Also:
        String.valueOf(Object)
      • append

        public StringBuffer append(char ch)
        Adds the specified character to the end of this buffer.
        Parameters:
        ch - the character to append.
        Returns:
        this StringBuffer.
        See Also:
        String.valueOf(char)
      • append

        public StringBuffer append(boolean b)
        Adds the string representation of the specified boolean to the end of this StringBuffer.

        If the argument is true the string "true" is appended, otherwise the string "false" is appended.

        Parameters:
        b - the boolean to append.
        Returns:
        this StringBuffer.
        See Also:
        String.valueOf(boolean)
      • delete

        public StringBuffer delete(int start,
                          int end)
        Deletes a range of characters.
        Parameters:
        start - the offset of the first character.
        end - the offset one past the last character.
        Returns:
        this StringBuffer.
        Throws:
        StringIndexOutOfBoundsException - if start < 0, start > end or end > length().
      • deleteCharAt

        public StringBuffer deleteCharAt(int location)
        Deletes the character at the specified offset.
        Parameters:
        location - the offset of the character to delete.
        Returns:
        this StringBuffer.
        Throws:
        StringIndexOutOfBoundsException - if location < 0 or location >= length()
      • insert

        public StringBuffer insert(int index,
                          String string)
        Inserts the string into this buffer at the specified offset.

        If the specified string is null, the string "null" is inserted, otherwise the contents of the string is inserted.

        Parameters:
        index - the index at which to insert.
        string - the string to insert (may be null).
        Returns:
        this buffer.
        Throws:
        StringIndexOutOfBoundsException - if index < 0 or index > length().
      • insert

        public StringBuffer insert(int index,
                          boolean b)
        Inserts the string representation of the specified boolean into this buffer at the specified offset.
        Parameters:
        index - the index at which to insert.
        b - the boolean to insert.
        Returns:
        this buffer.
        Throws:
        StringIndexOutOfBoundsException - if index < 0 or index > length().
      • insert

        public StringBuffer insert(int index,
                          char ch)
        Inserts the character into this buffer at the specified offset.
        Parameters:
        index - the index at which to insert.
        ch - the character to insert.
        Returns:
        this buffer.
        Throws:
        ArrayIndexOutOfBoundsException - if index < 0 or index > length().
      • insert

        public StringBuffer insert(int index,
                          char[] chars)
        Inserts the character array into this buffer at the specified offset.
        Parameters:
        index - the index at which to insert.
        chars - the character array to insert.
        Returns:
        this buffer.
        Throws:
        StringIndexOutOfBoundsException - if index < 0 or index > length().
        NullPointerException - if chars is null.
      • insert

        public StringBuffer insert(int index,
                          int i)
        Inserts the string representation of the specified integer into this buffer at the specified offset.
        Parameters:
        index - the index at which to insert.
        i - the integer to insert.
        Returns:
        this buffer.
        Throws:
        StringIndexOutOfBoundsException - if index < 0 or index > length().
      • insert

        public StringBuffer insert(int index,
                          long l)
        Inserts the string representation of the specified long into this buffer at the specified offset.
        Parameters:
        index - the index at which to insert.
        l - the long to insert.
        Returns:
        this buffer.
        Throws:
        StringIndexOutOfBoundsException - if index < 0 or index > length().
      • insert

        public StringBuffer insert(int index,
                          Object obj)
        Inserts the string representation of the specified object into this buffer at the specified offset.

        If the specified object is null, the string "null" is inserted, otherwise the objects toString method is used to get its string representation.

        Parameters:
        index - the index at which to insert.
        obj - the object to insert (may be null).
        Returns:
        this buffer.
        Throws:
        StringIndexOutOfBoundsException - if index < 0 or index > length().
      • capacity

        public int capacity()
        Returns the number of characters that can be held without growing.
        Returns:
        the capacity
        See Also:
        ensureCapacity(int), length()
      • charAt

        public char charAt(int index)
        Retrieves the character at the index.
        Parameters:
        index - the index of the character to retrieve.
        Returns:
        the char value.
        Throws:
        IndexOutOfBoundsException - if index is negative or greater than or equal to the current length().
      • setCharAt

        public void setCharAt(int index,
                     char ch)
        Sets the character at the index.
        Parameters:
        index - the zero-based index of the character to replace.
        ch - the character to set.
        Throws:
        IndexOutOfBoundsException - if index is negative or greater than or equal to the current length().
      • getChars

        public void getChars(int start,
                    int end,
                    char[] dest,
                    int destStart)
        Copies the requested sequence of characters to the char[] passed starting at destStart.
        Parameters:
        start - the inclusive start index of the characters to copy.
        end - the exclusive end index of the characters to copy.
        dest - the char[] to copy the characters to.
        destStart - the inclusive start index of dest to begin copying to.
        Throws:
        IndexOutOfBoundsException - if the start is negative, the destStart is negative, the start is greater than end, the end is greater than the current length() or destStart + end - begin is greater than dest.length.
      • ensureCapacity

        public void ensureCapacity(int min)
        Ensures that this object has a minimum capacity available before requiring the internal buffer to be enlarged. The general policy of this method is that if the minimumCapacity is larger than the current capacity(), then the capacity will be increased to the largest value of either the minimumCapacity or the current capacity multiplied by two plus two. Although this is the general policy, there is no guarantee that the capacity will change.
        Parameters:
        min - the new minimum capacity to set.
      • length

        public int length()
        The current length.
        Returns:
        the number of characters contained in this instance.
      • substring

        public String substring(int start)
        Returns the String value of the subsequence from the start index to the current end.
        Parameters:
        start - the inclusive start index to begin the subsequence.
        Returns:
        a String containing the subsequence.
        Throws:
        StringIndexOutOfBoundsException - if start is negative or greater than the current length().
      • substring

        public String substring(int start,
                       int end)
        Returns the String value of the subsequence from the start index to the end index.
        Parameters:
        start - the inclusive start index to begin the subsequence.
        end - the exclusive end index to end the subsequence.
        Returns:
        a String containing the subsequence.
        Throws:
        StringIndexOutOfBoundsException - if start is negative, greater than end or if end is greater than the current length().
      • toString

        public String toString()
        Returns the current String representation.
        Overrides:
        toString in class Object
        Returns:
        a String containing the characters in this instance.
      • setLength

        public void setLength(int length)
        Sets the current length to a new value. If the new length is larger than the current length, then the new characters at the end of this object will contain the char value of