Java StringBuilder Methods

About This Index
This page is the Java StringBuilder Methods index: all 24 core methods of the java.lang.StringBuilder class (since Java 1.5), each with syntax, overloads, parameters, return values, exception rules, three runnable Java 8+ examples, and FAQs.
Learn what a StringBuilder is on the StringBuilder hub. Use this page when you need a specific method such as append(), insert(), replace(), or toString().
Unlike String, a StringBuilder edits its own internal buffer without creating a new object on every call. Most editing methods return this so you can chain calls, while setCharAt(), setLength(), ensureCapacity(), trimToSize(), and getChars() return void.
How to Call a StringBuilder Method
Create an instance with new StringBuilder(...) (no import needed), then call instance methods on it:
StringBuilder sb = new StringBuilder("Java");
sb.append(" 8").insert(4, " SE"); // "Java SE 8" (chaining returns this)
sb.replace(5, 7, "Core"); // "Java Core 8"
int pos = sb.indexOf("Core"); // 5
String result = sb.toString(); // immutable String snapshot Java StringBuilder Methods Table
All method guides in this Java tutorial series (24 tutorials), grouped by purpose.
Add text
3 methodsAppend values at the end, append a Unicode code point, or insert text at any offset from 0 to length().
| Method | Description | Tutorial |
|---|---|---|
append() | Add text, numbers, booleans, characters, or char arrays onto the end in place and return this. | Try it |
appendCodePoint() | Append a full Unicode code point (1 or 2 UTF-16 chars) onto the end of the builder. | Try it |
insert() | Insert text, numbers, characters, or arrays at any offset from 0 to length() inclusive. | Try it |
Change and remove text
6 methodsDelete ranges or single characters, replace slices in place, reverse characters, overwrite one char, or change length.
| Method | Description | Tutorial |
|---|---|---|
delete() | Remove a range of characters from start up to end (end is clamped to length()). | Try it |
deleteCharAt() | Remove a single char at an index and shift the remaining characters left. | Try it |
replace() | Replace characters from start to end with a String (the builder grows or shrinks to fit). | Try it |
reverse() | Reverse the characters in place, keeping valid surrogate pairs together in the right order. | Try it |
setCharAt() | Overwrite one char at an index in place (returns void, so it cannot be chained). | Try it |
setLength() | Truncate the builder or pad it with NUL (\u0000) chars to reach newLength (returns void). | Try it |
Read and search
8 methodsRead characters or slices, search forward or backward for a substring, copy into a char[], check length, or convert to String.
| Method | Description | Tutorial |
|---|---|---|
charAt() | Read the single UTF-16 char at a zero-based index without changing the builder. | Try it |
indexOf() | Find the starting index of the first matching String substring, or -1 if absent. | Try it |
lastIndexOf() | Find the starting index of the last matching String substring, or -1 if absent. | Try it |
substring() | Extract part or all of the builder as a brand-new immutable String. | Try it |
subSequence() | Return a CharSequence slice for [start, end) without modifying the builder. | Try it |
getChars() | Copy a slice of characters from the builder directly into an existing char[] array. | Try it |
length() | Return the current number of UTF-16 char units stored in the builder. | Try it |
toString() | Create a new String copy of the current builder contents. | Try it |
Unicode code points
4 methodsWork accurately with supplementary characters and emoji (surrogate pairs) instead of individual UTF-16 units.
| Method | Description | Tutorial |
|---|---|---|
codePointAt() | Return the full Unicode code point starting at a given UTF-16 index. | Try it |
codePointBefore() | Return the Unicode code point that ends just before a given UTF-16 index. | Try it |
codePointCount() | Count the Unicode code points in a range, treating each surrogate pair as one character. | Try it |
offsetByCodePoints() | Move a UTF-16 index forward or backward by a given number of Unicode code points. | Try it |
Capacity
3 methodsInspect how much buffer space is reserved, pre-allocate space before large builds, or trim unused space afterward.
| Method | Description | Tutorial |
|---|---|---|
capacity() | Return how many char units the internal buffer can hold before allocating a larger array. | Try it |
ensureCapacity() | Request that the internal buffer hold at least minimumCapacity chars without resizing again. | Try it |
trimToSize() | Ask the builder to shrink its internal storage buffer down toward length(). | Try it |
Frequently Asked Questions
Start with append()
Add strings, numbers, booleans, and chars to a builder in place, and chain calls.
12 people found this page helpful
