String Buffer
String Buffer
For example:
x = "a" + 4 + "c"
x = new StringBuffer().append("a").append(4).
append("c") .toString()
Why StringBuffer?
StringBuffer is a peer class of String that provides much of the
functionality of strings.
◦ StringBuffer( )
◦ StringBuffer(int size)
◦ StringBuffer(String str)
◦ StringBuffer(CharSequence chars)
When we increase the size of the buffer, null characters are added to
the end of the existing buffer.
class insertDemo {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("I Java!");
sb.insert(2, "like ");
System.out.println(sb);
}
}
reverse( )
Used to reverse the characters within a StringBuffer object.
This method returns the reversed object on which it was called.
StringBuffer reverse()
Example:
class ReverseDemo {
public static void main(String args[]) {
StringBuffer s = new StringBuffer(“Banana");
System.out.println(s);
s.reverse();
System.out.println(s);
}
}
delete( ) and deleteCharAt( )
Used to delete characters within a StringBuffer.