StringBuffer class

What is StringBuffer

String represents fixed-length, immutable character sequences.

StringBuffer represents growable and writeable character sequences.

StringBuffer will automatically grow to make room for such additions.

Java manipulate StringBuffers behind the scenes when you are using the overloaded + operator.

Create StringBuffer object

ConstructorSummary
StringBuffer()Creates a string buffer with no characters in it and an initial capacity of 16 characters.
StringBuffer(CharSequence seq)Creates a string buffer that contains the same characters as the specified CharSequence.
StringBuffer(int capacity)Creates a string buffer with no characters in it and the specified initial capacity.
StringBuffer(String str)Creates a string buffer initialized to the contents of the specified string.

Append data to the end of a StringBuffer

ReturnMethodSummary
StringBufferappend(boolean b)Appends the boolean argument to the sequence.
StringBufferappend(char c)Appends the char argument to this sequence.
StringBufferappend(char[] str)Appends the char array argument to this sequence.
StringBufferappend(char[] str, int offset, int len)Appends a subarray of the char array argument to this sequence.
StringBufferappend(CharSequence s)Appends the CharSequence to this sequence.
StringBufferappend(CharSequence s, int start, int end)Appends a subsequence of the CharSequence to this sequence.
StringBufferappend(double d)Appends the double argument to this sequence.
StringBufferappend(float f)Appends the float argument to this sequence.
StringBufferappend(int i)Appends the int argument to this sequence.
StringBufferappend(long lng)Appends the long argument to this sequence.
StringBufferappend(Object obj)Appends the Object argument.
StringBufferappend(String str)Appends this character sequence.
StringBufferappend(StringBuffer sb)Appends the StringBuffer to this sequence.
StringBufferappendCodePoint(int codePoint)Appends the codePoint argument to this sequence.
StringBufferinsert(int offset, boolean b)Inserts the boolean argument into this sequence.
StringBufferinsert(int offset, char c)Inserts the char argument into this sequence.
StringBufferinsert(int offset, char[] str)Inserts the char array argument into this sequence.
StringBufferinsert(int index, char[] str, int offset, int len)Inserts a subarray of the str array argument into this sequence.
StringBufferinsert(int dstOffset, CharSequence s)Inserts CharSequence into this sequence.
StringBufferinsert(int dstOffset, CharSequence s, int start, int end)Inserts a subsequence of the specified CharSequence into this sequence.
StringBufferinsert(int offset, double d)Inserts the double argument into this sequence.
StringBufferinsert(int offset, float f)Inserts the float argument into this sequence.
StringBufferinsert(int offset, int i)Inserts the second int argument into this sequence.
StringBufferinsert(int offset, long l)Inserts the long argument into this sequence.
StringBufferinsert(int offset, Object obj)Inserts the Object argument into this character sequence.
StringBufferinsert(int offset, String str)Inserts str.

Delete or replace or reverse a StringBuffer

ReturnMethodSummary
StringBufferdelete(int start, int end)Removes the characters in a substring of this sequence.
StringBufferdeleteCharAt(int index)Removes the char at the specified position in this sequence.
StringBufferreplace(int start, int end, String str)Replaces the characters in a substring of this sequence with characters in the specified String.
StringBufferreverse()Causes this character sequence to be replaced by the reverse of the sequence.

Capacity and length

ReturnMethodSummary
intcapacity()Returns the current capacity.
intlength()Returns the length (character count).
voidensureCapacity(int minimumCapacity)Ensures that the capacity is at least equal to the specified minimum.
voidsetLength(int newLength)Sets the length of the character sequence.
voidtrimToSize()Attempts to reduce storage used for the character sequence.

Char index

ReturnMethodSummary
charcharAt(int index)Returns the char value in this sequence at the specified index.
voidsetCharAt(int index, char ch)The character at the specified index is set to ch.
voidgetChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)Characters are copied from this sequence into the destination character array dst.
intindexOf(String str)Returns the index within this string of the first occurrence of the specified substring.
intindexOf(String str, int fromIndex)Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
intlastIndexOf(String str)Returns the index within this string of the rightmost occurrence of the specified substring.
intlastIndexOf(String str, int fromIndex)Returns the index within this string of the last occurrence of the specified substring.

Convert StringBuffer to String

ReturnMethodSummary
Stringsubstring(int start)Returns a new String that contains a subsequence of characters currently contained in this character sequence.
Stringsubstring(int start, int end)Returns a new String that contains a subsequence of characters currently contained in this sequence.
StringtoString()Returns a string representing the data in this sequence.
CharSequencesubSequence(int start, int end)Returns a new character sequence that is a subsequence of this sequence.
Revised from Open JDK source code
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.