StringBuffer is a peer class of String that provides much of the functionality of the strings. String represents fixed-length, immutable character sequences.In contrast StringBuffer represents growable and writable character sequences. String buffers are preferred when heavy modification of character strings is involved (appending,inserting, deleting, modifying etc).Strings can be obtained from string buffers. Since the StringBuffer class does not override the equals() method from the Object class, contents of string buffers should be converted to String objects for string comparison.A StringIndexOutOfBoundsException is thrown if an index is not valid when using wrong index in String Buffer manipulations.The StringBuffer
provides 3 constructors which create, initialize and set the initial capacity of StringBuffer objects.
StringBuffer Constructors
public class TestStringBuffer {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("Test StringBuffer");
StringBuffer stringBufferWithFixedSize = new StringBuffer(500);
StringBuffer stringBufferWithDefaultSize = new StringBuffer(); //Default Constructor
System.out.println("StringBuffer : " + stringBuffer);
System.out.println("StringBuffer with Fixed Size capacity : " + stringBufferWithFixedSize.capacity());
System.out.println("StringBuffer with Default Size capacity : " + stringBufferWithDefaultSize.capacity());
}
}
StringBuffer : Test StringBuffer StringBuffer with Fixed Size capacity : 500 StringBuffer with Default Size capacity : 16
String Buffer Functions
- capacity() -:
Returns the current capacity
of the String buffer. - length() :-
Returns the length (character count)
of this string buffer. - charAt(int index) :-
The specified character of the sequence currently
represented by the string buffer, as indicated by the index argument, is returned. - setCharAt(int index, char ch) :-
The character at the
specified index of this string buffer is set to ch - toString() :-
Converts to a string representing the data
in this string buffer - insert(int offset, char c) :-
Inserts the string representation
of the char argument into this string buffer. - delete(int start, int end) :-
Removes the characters in a substring
of this StringBuffer - replace(int start, int end, String str) :-
Replaces the
characters in a substring of this StringBuffer with characters in the specified String. - reverse() :-
The character sequence contained in this string buffer
is replaced by the reverse of the sequence. - append(String str) :-
Appends the string to this string buffer.
public class TestStringBuffer {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("Test String Buffer");
StringBuffer stringBufferWithFixedSize = new StringBuffer(100);
StringBuffer stringBufferWithDefaultSize = new StringBuffer();
//StringBuffer function capacity()
System.out.println("StringBuffer : " + stringBuffer);
System.out.println("StringBuffer capacity : " + stringBuffer.capacity());
System.out.println("StringBuffer with Fixed Size capacity : " + stringBufferWithFixedSize.capacity());
System.out.println("StringBuffer with Default Size capacity : " + stringBufferWithDefaultSize.capacity());
//StringBuffer function length()
System.out.println("stringBuffer length : " + stringBuffer.length());
//StringBuffer function charAt()
System.out.println("stringBuffer charAt 2 : " + stringBuffer.charAt(2));
//StringBuffer function setCharAt()
stringBuffer.setCharAt(1, 'E');
System.out.println("stringBuffer after setCharAt 1 to E is : "+ stringBuffer);
//StringBuffer function toString()
System.out.println("StringBstringBufferuffer toString() is : " + stringBuffer.toString());
//StringBuffer function append()
stringBufferWithDefaultSize.append("Test Buffer");
System.out.println("stringBufferWithDefaultSize when appended with a String : "+ stringBufferWithDefaultSize.toString());
//StringBuffer function insert()
stringBufferWithDefaultSize.insert(1, 'C');
System.out.println("stringBufferWithDefaultSize when C is inserted at 1 : " + stringBufferWithDefaultSize.toString());
//StringBuffer function delete()
stringBufferWithDefaultSize.delete(1, 'C');
System.out.println("stringBufferWithDefaultSize when C is deleted at 1 : "+ stringBufferWithDefaultSize.toString());
//StringBuffer function reverse()
stringBufferWithDefaultSize.reverse();
System.out.println("Reversed stringBufferWithDefaultSize : " + stringBufferWithDefaultSize);
//StringBuffer function setLength()
stringBufferWithFixedSize.setLength(5);
stringBufferWithFixedSize.append("Sample Buffer");
System.out.println("stringBufferWithFixedSize : " + stringBufferWithFixedSize);
}
}
Output:
StringBuffer : Test String Buffer StringBuffer capacity : 34 StringBuffer with Fixed Size capacity : 100 StringBuffer with Default Size capacity : 16 stringBuffer length : 18 stringBuffer charAt 2 : s stringBuffer after setCharAt 1 to E is : TEst String Buffer StringBstringBufferuffer toString() is : TEst String Buffer stringBufferWithDefaultSize when appended with a String : Test Buffer stringBufferWithDefaultSize when C is inserted at 1 : TCest Buffer stringBufferWithDefaultSize when C is deleted at 1 : T Reversed stringBufferWithDefaultSize : T stringBufferWithFixedSize : Sample Buffer