Java StringBuffer class is used to create mutable (modifiable) string. The StringBuffer class in java is same as String class except it is mutable i.e. it can be changed.
A string that can be modified or changed is known as mutable string. StringBuffer and StringBuilder classes are used for creating mutable string.
Important Constructors of StringBuffer class
Constructor |
Description |
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) |
creates an empty string buffer with the specified capacity as length. |
Important methods of StringBuffer class
Method |
Description |
append(String s) |
is used to append the specified string with this string. The append() method is overloaded like append(char), append(boolean), append(int), append(float), append(double) etc. |
insert(int offset, String s) |
is used to insert the specified string with this string at the specified position. The insert() method is overloaded like insert(int, char), insert(int, boolean), insert(int, int), insert(int, float), insert(int, double) etc. |
replace(int startIndex, int endIndex, String str) |
is used to replace the string from specified startIndex and endIndex. |
delete(int startIndex, int endIndex) |
is used to delete the string from specified startIndex and endIndex. |
reverse() |
is used to reverse the string. |
capacity() |
is used to return the current capacity. |
ensureCapacity(int minimumCapacity) |
is used to ensure the capacity at least equal to the given minimum. |
charAt(int index) |
is used to return the character at the specified position. |
length() |
is used to return the length of the string i.e. total number of characters. |
substring(int beginIndex) |
is used to return the substring from the specified beginIndex. |
substring(int beginIndex, int endIndex) |
is used to return the substring from the specified beginIndex and endIndex. |
1) StringBuffer append() method
The append() method concatenates the given argument with this string.
class StringBufferExample{
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
}
}
2) StringBuffer insert() method
The insert() method inserts the given string with this string at the given position.
class StringBufferExample2{
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
}
}
3) StringBuffer replace() method
The replace() method replaces the given string from the specified beginIndex and endIndex.
class StringBufferExample3{
public static void main(String args[]){ StringBuffer sb=new StringBuffer("Hello"); sb.replace(1,3,"Java"); System.out.println(sb);//prints HJavalo
}
}
4) StringBuffer delete() and deleteCharAt() method
The delete() method of StringBuffer class deletes the string from the specified beginIndex to endIndex.
class StringBufferExample4{
public static void main(String args[]){ StringBuffer sb=new StringBuffer("Hello"); sb.delete(1,3); System.out.println(sb);//prints Hlo
}
}
5) StringBuffer reverse() method
The reverse() method of StringBuilder class reverses the current string.
class StringBufferExample5{
public static void main(String args[]){ StringBuffer sb=new StringBuffer("Hello"); sb.reverse(); System.out.println(sb);//prints olleH
}
}
6) charAt( ) and setCharAt( )
The value of a single character can be obtained from a StringBuffer via the charAt( ) method. You can set the value of a character within a StringBuffer using setCharAt( ).
7) setLength( )
To set the length of the buffer within a StringBuffer object, use setLength( ). Its general form is shown here:
void setLength(int len)
Here, len specifies the length of the buffer. This value must be nonnegative.
8) ensureCapacity( )
If you want to preallocate room for a certain number of characters after a StringBuffer has been constructed, you can use ensureCapacity( ) to set the size of the buffer. This is useful if you know in advance that you will be appending a large number of small strings to a StringBuffer.
ensureCapacity( ) has this general form:
void ensureCapacity(int capacity)
Here, capacity specifies the size of the buffer.
9) length( ) and capacity( )
The current length of a StringBuffer can be found via the length( ) method, while the total allocated capacity can be found through the capacity( ) method. They have the following general forms:
int length( )
int capacity( )
Output
buffer = Hello length = 5
capacity = 21
Since sb is initialized with the string “Hello” when it is created, its length is 5. Its capacity is 21 because room for 16 additional characters is automatically added.