Essential StringBuilder Methods in Java
Core StringBuilder Methods and Usage
| Method | Parameters | Description |
|---|---|---|
append() |
Any primitive, String, char, or object |
Appends the string representation of the argument to the end of the builder. |
capacity() |
None | Returns the current capacity of the internal buffer. |
charAt(int index) |
Index position | Returns the character at the specified index. |
codePointAt(int index) |
Index position | Returns the Unicode code point at the given index. |
codePointBefore(int index) |
Index position | Returns the Unicode code point before the given index. |
codePointCount(int beginIndex, int endIndex) |
Start and end indices | Counts Unicode code points in the specified range. |
delete(int start, int end) |
Start (inclusive) and end (exclusive) indices | Removes characters in the specified range. |
deleteCharAt(int index) |
Index position | Deletes the character at the specified index. |
ensureCapacity(int min) |
Minimum desired capacity | Ensures the buffer can hold atleast min characters. |
getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) |
Source range, destination array, and offset | Copies characters into a provided character array. |
indexOf(String str) |
Target substring | Returns the first index where the substring appears. |
insert(int offset, ...) |
Insertion index and value to insert | Inserts the string representation of the value at the offset. |
lastIndexOf(String str) |
Target substring | Returns the last index where the substring appears. |
length() |
None | Returns the number of characters currently in the builder. |
offsetByCodePoints(int index, int codePointOffset) |
Starting index and offset in code points | Computes the index offset by a number of Unicode code points. |
replace(int start, int end, String str) |
Start, end, and replacement string | Replaces characters in the range with the new string. |
reverse() |
None | Reverses the sequence of characters in place. |
setCharAt(int index, char ch) |
Index and new character | Sets the character at the specified index. |
setLength(int newLength) |
New length | Truncates or pads the builder to the specified length. |
substring(int start) |
Start index | Returns a substring from the start index to the end. |
toString() |
None | Converts the content to an immutable String. |
trimToSize() |
None | Adjusts internal capacity to match the current length. |
Practical Examples
Appending values:
StringBuilder buffer = new StringBuilder();
buffer.append("Java").append(' ').append(17);
System.out.println(buffer); // Output: Java 17
Inserting content:
StringBuilder text = new StringBuilder("HelloWorld");
text.insert(5, " ");
System.out.println(text); // Output: Hello World
Deleting a segment:
StringBuilder phrase = new StringBuilder("Remove this part");
phrase.delete(7, 11);
System.out.println(phrase); // Output: Remove part
Replacing a section:
StringBuilder message = new StringBuilder("Good morning!");
message.replace(5, 12, "evening");
System.out.println(message); // Output: Good evening!
Reversing content:
StringBuilder word = new StringBuilder("example");
word.reverse();
System.out.println(word); // Output: elpmaxe
Converting to String:
StringBuilder builder = new StringBuilder("final result");
String result = builder.toString();
System.out.println(result); // Output: final result