![]() |
VOOZH | about |
StringBuffer class in Java represents a sequence of characters that can be modified, which means we can change the content of the StringBuffer without creating a new object every time. It represents a mutable sequence of characters.
Example: Here is an example of using StringBuffer to concatenate strings.
Hello world
The diagram shows the interfaces implemented by StringBuffer and illustrates its relationship with Appendable, CharSequence, and Serializable in Java.
Example:
Default Constructor: Hello With Capacity 50: Java Programming With String: Welcome to Java
append() method concatenates the given argument with this string.
Hello Java
insert() method inserts the given string with this string at the given position.
HJavaello
replace() method replaces the given string from the specified beginIndex and endIndex-1.
HJavalo
delete() method is used to delete the string from the specified beginIndex to endIndex-1.
Hlo
reverse() method of the StringBuffer class reverses the current string.
olleH
capacity() method of the StringBuffer class returns the current capacity of the buffer. The default capacity of the buffer is 16. If the number of characters increases from its current capacity, it increases the capacity by (oldcapacity*2)+2.
For example, if the current capacity is 16, it will be (16*2)+2=34.
16 16 34
This method return the number of character in given string.
Length of string GeeksforGeeks=13
Methods | Description | Syntax |
|---|---|---|
| append() | Used to add text at the end of the existing text. | sb.append(String str); |
| length() | The length of a StringBuffer can be found by the length( ) method. | int len = sb.length(); |
| capacity() | the total allocated capacity can be found by the capacity( ) method. | int cap = sb.capacity(); |
| charAt() | This method returns the char value in this sequence at the specified index. | char ch = sb.charAt(int index); |
| delete() | Deletes a sequence of characters from the invoking object. | sb.delete(int start, int end); |
| deleteCharAt() | Deletes the character at the index specified by the loc. | sb.deleteCharAt(int index); |
| ensureCapacity() | Ensures capacity is at least equal to the given minimum. | sb.ensureCapacity(int minimumCapacity); |
| insert() | Inserts text at the specified index position. | sb.insert(int offset, String str); |
| reverse() | Reverse the characters within a StringBuffer object. | sb.reverse(); |
| replace() | Replace one set of characters with another set inside a StringBuffer object. | sb.replace(int start, int end, String str); |
Increases StringBuffer capacity to the specified value | void ensureCapacity(int capacity) | |
Appends code point as a string to the sequence. | public StringBuffer appendCodePoint(int codePoint) | |
charAt(int index) | Returns the char at the specified index. | public char charAt(int index) |
IntStream chars() | Returns a stream of int values from zero-extended chars in the sequence.. | public IntStream chars() |
Returns the character (Unicode code point) at the specified index. | public int codePointAt(int index) | |
Returns the Unicode code point before the given index. | public int codePointBefore(int index) | |
Returns the count of Unicode code points in the specified text range. | public int codePointCount(int beginIndex, int endIndex) | |
IntStream codePoints() | Returns a stream of code points from the sequence. | public IntStream codePoints() |
Copies characters from the sequence into the destination array. | public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) | |
Returns the index of the first occurrence of the specified substring. | public int indexOf(String str) | |
Returns the index of the last occurrence of the specified substring. | public int lastIndexOf(String str) | |
Returns the index offset by a specified number of code points from the given index.. | public int offsetByCodePoints(int index, int codePointOffset) | |
In this method, the character at the specified index is set to ch. | public void setCharAt(int index, char ch) | |
This method sets the length of the character sequence. | public void setLength(int newLength) | |
Returns index offset by given code points from the specified index. | public CharSequence subSequence(int start, int end) | |
Returns a new String containing a subsequence of this character sequence. | public String substring(int start) | |
Returns a string representing the data in the sequence. | public String toString() | |
Attempts to minimize storage used by the character sequence. | public void trimToSize() |