StringBuffer and StringBuilder in java is a class that provides the mutable(changeable) ability.
Since a String is immutable that is its value cannot be changed once the object is created. So to modify the String, StringBuffer and StringBuilder class is used.
Both these classes work fine but the difference is that the StringBuffer is thread-safe whereas StringBuilder is not.
- Far a faster process, consider using StringBuilder because it is faster than StringBuffer.
- For safety, purposes consider using StringBuilder.
StringBuffer Methods:
1. StringBuffer append() method:
This method concatenates the two string and updates the result. The method takes boolean, char, int, long, Strings, etc.
Example for StringBuffer append() in Java:
1 2 3 4 5 6 7 8 9 | public class StringBufferTest { public static void main(String args[]) { StringBuffer s = new StringBuffer("Long "); s.append("Day");//changed with Day System.out.println(s);//prints the concatenated result } } |
Output:
1 | Long Day |
2. StringBuffer insert() method:
This String inserts the new string in a specified position. We need to offset the position before the value as shown in an example.
Example for StringBuffer insert() in Java:
1 2 3 4 5 6 7 8 9 10 | public class StringBufferTest { public static void main(String args[]) { StringBuffer s = new StringBuffer("Long "); s.insert(1,"Day");//now original string is changed System.out.println(s);//prints HJavaello } } |
Output:
1 | LDayong |
3. StringBuffer delete() method:
As the name suggest, this method is used to remove/delete the string by index number. We need to specify the beginIndex and endIndex.
Example for StringBuffer delete() in Java:
1 2 3 4 5 6 7 8 9 10 | public class StringBufferTest { public static void main(String args[]) { StringBuffer s = new StringBuffer("ABCDEFGHIJK"); s.delete(4, 8); System.out.println(s); } } |
Output:
1 | ABCDIJK |
4. StringBuffer replace() method:
This method is used to replace a String with another String within a specified index.
This content three values one to remove from beginIndex to endIndex and then the String value for replacement.
Example for StringBuffer replace() in Java:
1 2 3 4 5 6 7 8 9 10 | public class StringBufferTest { public static void main(String args[]) { StringBuffer s = new StringBuffer("ABCDEFGHIJKJ"); s.replace(4, 9, "xyz"); System.out.println(s); } } |
Output:
1 | ABCDxyzJKJ |
5. StringBuffer reverse() method:
This method reverses the value of the string that is invoked.
Example for StringBuffer reverse() in Java:
1 2 3 4 5 6 7 8 9 10 | public class StringBufferTest { public static void main(String args[]) { StringBuffer s = new StringBuffer("Sting Buffer"); s.reverse(); System.out.println(s); } } |
Output:
1 | reffuB gnirtS |
List of some other methods that are similar to string class:
- void ensureCapacity(int minimumCapacity):
This method ensures that the buffer capacity is at least to minimum capacity. - int capacity():
It returns the current capacity of the string buffer. - int indexOf(String str):
The index of the first occurrence of the specified substring is returned within this string. - int indexOf(String str, int fromIndex):
The index of the first occurrence of the specified substring is returned within this string and it starts from the index specified. - int lastIndexOf(String str, int fromIndex):
The index of the last occurrence of the specified substring is returned within this string and from the specified index. - void setLength(int newLength):
This method sets the length of the string buffer. - int length():
It is used to return the length of the string buffer. - String toString():
The data represented to this string buffer is converted to a string through this method - char charAt(int index):
It returns the sequence currently represented by the string buffer that is specified the index argument. - CharSequence subSequence(int start, int end):
This method returns a new character sequence that is a sub-part of this sequence.
There are more String methods such as, String substring(int start, int end), String substring(int start), void setCharAt(int index, char ch), int lastIndexOf(String str), void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin).