Objects of StringBuffer represents mutable strings. Each StringBuffer object creates an array of default or specified capacity that is used to hold the contents of StringBuffer.StringBuffer can be changed dynamically. Default capacity of StringBuffer object is 16 characters.
StringBuffer objects can be created using following constructors:-
|
public class StringBufferDemo { public static void main(String[] args) { // Examples of Creation of Strings StringBuffer strBuf1 = new StringBuffer("Bobby"); StringBuffer strBuf2 = new StringBuffer(100); // With capacity 100 StringBuffer strBuf3 = new StringBuffer(); // Default Capacity 16 System.out.println("strBuf1 : " + strBuf1); System.out.println("strBuf1 capacity : " + strBuf1.capacity()); System.out.println("strBuf2 capacity : " + strBuf2.capacity()); System.out.println("strBuf3 capacity : " + strBuf3.capacity()); System.out.println("strBuf1 length : " + strBuf1.length()); System.out.println("strBuf1 charAt 2 : " + strBuf1.charAt(2)); // A StringIndexOutOfBoundsException is thrown if the index is not // valid. strBuf1.setCharAt(1, 't'); System.out.println("strBuf1 after setCharAt 1 to t is : " + strBuf1); System.out.println("strBuf1 toString() is : " + strBuf1.toString()); strBuf3.append("beginner-java-tutorial"); System.out.println("strBuf3 when appended with a String : " + strBuf3.toString()); strBuf3.insert(1, 'c'); System.out.println("strBuf3 when c is inserted at 1 : " + strBuf3.toString()); strBuf3.delete(1, 'c'); System.out.println("strBuf3 when c is deleted at 1 : " + strBuf3.toString()); strBuf3.reverse(); System.out.println("Reversed strBuf3 : " + strBuf3); strBuf2.setLength(5); strBuf2.append("jdbc-tutorial"); System.out.println("strBuf2 : " + strBuf2); // We can clear a StringBuffer using the following line strBuf2.setLength(0); System.out.println("strBuf2 when cleared using setLength(0): " + strBuf2); } }
append ( ) method is used to append a primitive value or contents of a string to the StringBuffer.The append() method concatenates the given argument with this string.
class Test { public static void main(String args[]) { StringBuffer sb = new StringBuffer("Hello "); sb.append("Java");// now original string is changed System.out.println(sb);// prints Hello Java } }
insert( )The insert() method is used to insert a string at the given position in the StringBuffer object.
public StringBuffer insert(int index, String s);
class Test { public static void main(String args[]) { StringBuffer sb = new StringBuffer("Hello "); sb.insert(1, "Java");// now original string is changed System.out.println(sb);// prints HJavaello } }
replace( ) method is used to replace part of a StringBuffer object with the given string.
public StringBuffer replace(int startIndex, int endIndex, String s);
class Test { public static void main(String args[]) { StringBuffer sb = new StringBuffer("Hello"); sb.replace(1, 3, "NewDelhi"); System.out.println(sb); } }
reverse ( ) method reverses the contents of StringBuffer.
public StringBuffer reverse();
toString( ) method returns the content of StringBuffer as an immutable string.
public StringBuffer toString();