StringBuffer class in java
StringBuffer class in java is like a String ,but which can be modified. The length of the String and content of sequence can be changed by using StringBuffer methods.
It can be implemented in following ways.
- StringBuffer(): creates an empty string buffer with the initial capacity of 16.
- StringBuffer(String str): creates a string buffer with the specified string.
- StringBuffer(int capacity): Capacity is the no. of character it can hold.It increases automatically as more contents added to it.
Important point to note:
It provides mutable String.
If String can be modified ,called a mutable String. It can be achieved by StringBuffer class.
Useful methods of StringBuffer:
1. StringBuffer append() method:
This method appends a new String into the existing String.
public class ExampleStringBuffer { public static void main(String[] args) { StringBuffer str = new StringBuffer("House"); System.out.println(str.append(" is beautiful")); } }
2. Length() method:
This method will give you the length of the String.
public class ExampleStringBuffer { public static void main(String[] args) { StringBuffer str = new StringBuffer("House"); System.out.println(str.length()); } }
3. replace() method:
This method will replace substring taken its start position and end position , with a new substring.
public class ExampleStringBuffer { public static void main(String[] args) { StringBuffer str = new StringBuffer("This is a House"); System.out.println(str.replace(5, 7, "was")); // results : This was a House } }
4. insert() method:
This method is used to insert a new String to an existing string.
public class ExampleStringBuffer { public static void main(String[] args) { StringBuffer str = new StringBuffer("This is a House"); System.out.println(str.insert(0, "Yes, ")); } }
5. subString() method:
It will return a substring extracted
public class ExampleStringBuffer { public static void main(String[] args) { StringBuffer str = new StringBuffer("This is a House"); System.out.println(str.substring(8)); } }
from a start point.
6. Delete() method:
This will delete a part of the String mentioned in the start and end point.
public class ExampleStringBuffer { public static void main(String[] args) { StringBuffer str = new StringBuffer("This is a House"); System.out.println(str.delete(0, 8)); } }
7. reverse() method:
This method returns a reversed String.
public class ExampleStringBuffer { public static void main(String[] args) { StringBuffer str = new StringBuffer("This is a House"); System.out.println(str.reverse()); } }
8. Capacity() method:
This method returns the current capacity of the buffer. The default capacity of the buffer is 16. If the number of character increases from its current capacity, it increases the capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be (16*2)+2=34.
public class ExampleStringBuffer { public static void main(String[] args) { StringBuffer str1 = new StringBuffer(); System.out.println(str1.capacity()); // returns 16 str1.append("House"); System.out.println(str1.capacity()); // returns 16 StringBuffer str2 = new StringBuffer("This is a House"); System.out.println(str2.capacity()); // returns 31 } }