StringBuffer
is a class in Java that represents a mutable sequence of characters. It provides several methods to manipulate and modify strings efficiently. Here are some of the important methods in the StringBuffer
class:
append(String str)
:
str
to the end of the current StringBuffer
object.StringBuffer
object.insert(int offset, String str)
:
str
at the given offset
position in the current StringBuffer
object.StringBuffer
object.delete(int start, int end)
:
start
index (inclusive) to the end
index (exclusive) from the current StringBuffer
object.StringBuffer
object.deleteCharAt(int index)
:
index
from the current StringBuffer
object.StringBuffer
object.replace(int start, int end, String str)
:
start
index (inclusive) to the end
index (exclusive) with the specified string str
.StringBuffer
object.reverse()
:
StringBuffer
object.StringBuffer
object.substring(int start)
and substring(int start, int end)
:
String
class.start
index to the end of the StringBuffer
.start
index (inclusive) to the end
index (exclusive).length()
:
StringBuffer
object.capacity()
:
StringBuffer
object.setLength(int newLength)
:
StringBuffer
to the specified newLength
.newLength
is greater than the current length, the StringBuffer
will be padded with null characters ('\0') to reach the desired length.newLength
is smaller than the current length, characters beyond the newLength
will be truncated.charAt(int index)
and setCharAt(int index, char ch)
:charAt(int index)
returns the character at the specified index
.setCharAt(int index, char ch)
sets the character at the specified index
to the given ch
.These are some of the most commonly used methods in the StringBuffer
class. Keep in mind that StringBuffer
is a mutable class, which means you can modify its contents and perform various string manipulation operations efficiently.