Java StringBuilder Methods

Beginner
6 min read
Updated: Nov 2026
24 method guides
Method index

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().

Mutable in place, not thread-safe.

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:

Java
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 methods

Append values at the end, append a Unicode code point, or insert text at any offset from 0 to length().

MethodDescriptionTutorial
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 methods

Delete ranges or single characters, replace slices in place, reverse characters, overwrite one char, or change length.

MethodDescriptionTutorial
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

Unicode code points

4 methods

Work accurately with supplementary characters and emoji (surrogate pairs) instead of individual UTF-16 units.

MethodDescriptionTutorial
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 methods

Inspect how much buffer space is reserved, pre-allocate space before large builds, or trim unused space afterward.

MethodDescriptionTutorial
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

They are the instance methods of java.lang.StringBuilder for building and editing text in place: adding text with append() and insert(), changing or deleting characters with delete(), replace(), reverse(), setCharAt(), and setLength(), searching with indexOf(), slicing with substring(), inspecting Unicode code points, and managing buffer capacity.
This index lists 24 guides, from append() and appendCodePoint() to toString() and trimToSize(), grouped into five categories.
They mutate the builder in place and return the same object reference (this), so you can chain calls like sb.append("Hi ").append(42).reverse(). Methods like setCharAt(), setLength(), ensureCapacity(), and trimToSize() return void instead.
String is immutable, so every edit creates a new String. StringBuilder is mutable and fast for single-threaded code. StringBuffer has the same API but its methods are synchronized for thread safety, making it slightly slower when only one thread uses it.
Read the Java StringBuilder hub first. Then open append(), insert(), replace(), delete(), and toString() from the tables below.

Start with append()

Add strings, numbers, booleans, and chars to a builder in place, and chain calls.

append() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful