String Buffer


In Java String are immutable, so we use String Buffer classes to make necessary changes in string objects. With the help of String Buffer Class we can alter, concat and modify string object again without even creating a lot of unused objects. We can initialize a string buffer by StringBuffer() Class →

StringBuffer sb = new StringBuffer("JAVA CODE ");

String Buffer provide methods to alter the string; some of the methods are listed below→

1)append() method:

The Append method is use to append the string at the end of an existing string buffer

2) insert() method:

The insert method is used to insert character sequence to the string buffer by any index, so it take two argument first is the index and second argument is the character sequence.

3) delete() method:

the delete method in string buffer is used to remove character from string based on start index and end index, it take two argument both index and return removed carachter from startindex to endindex-1

4) reverse() method:

The reverse method returns the existing string but in reverse order of index.

Example:

Com.knowledge2life public class Main{ public static void main(String args[]){ StringBuffer sb=new StringBuffer("Knowledge2"); sb.append("life"); System.out.println(sb); } }

OUTPUT:

Knowledge2life