In Java, sb.insert(offset, value) places text or a value's text form at offset, shifting later characters right as the buffer grows.
Every overload returns this for chaining. Offset 0 prepends at the start, and sb.length() appends at the end.
An instance method of the java.lang.StringBuilder class (since Java 1.5, JDK 1.5): a final, mutable sequence of UTF-16 characters that is not thread-safe. Call it on an existing builder, as in sb.insert(...). No import is needed because java.lang is always visible.
Syntax
Pass the zero-based target offset first, followed by the value or slice to insert.
Valid boundary: inserts at the very end, just like append.
sb.insert(index, arr, off, len)
Inserts a len-char slice from char[] arr at index.
Notice the parameter order in slice overloads: destination index in the builder comes first, followed by the source array or CharSequence and its range.
Library declarations
Java
// java.lang.StringBuilder
public StringBuilder insert(int offset, Object obj)
public StringBuilder insert(int offset, String str)
public StringBuilder insert(int offset, char[] str)
public StringBuilder insert(int index, char[] str, int offset, int len)
public StringBuilder insert(int dstOffset, CharSequence s)
public StringBuilder insert(int dstOffset, CharSequence s, int start, int end)
public StringBuilder insert(int offset, boolean b)
public StringBuilder insert(int offset, char c)
public StringBuilder insert(int offset, int i)
public StringBuilder insert(int offset, long l)
public StringBuilder insert(int offset, float f)
public StringBuilder insert(int offset, double d)
Parameters
Parameter
Description
offset / dstOffset / index (int)
Zero-based insertion point in the builder, from 0 to length() inclusive.
str / obj / s / primitive
Value to convert to text and insert at that offset.
start, end / offset, len
Optional source bounds when inserting part of a CharSequence or char[].
Return Value
Return Value
Description
StringBuilder
A reference to this builder for chaining further edits.
After null insert: Java-null
Out-of-bounds offset threw StringIndexOutOfBoundsException
How It Works
Chained end insert. Adding '-' at index 4 raises length to 5, so the second call reads the updated length().
Null becomes "null". Because nothing has static type String, Java picks insert(int, String) and inserts four letters.
Past length throws. Asking to insert at sb.length() + 1 violates the valid 0..length() range and throws StringIndexOutOfBoundsException.
Frequently Asked Questions
Any offset from 0 up to sb.length() inclusive. Offset 0 prepends to the start, and offset sb.length() appends to the end.
StringIndexOutOfBoundsException when offset is negative or greater than length(). The CharSequence slice overload throws IndexOutOfBoundsException for bad start/end bounds.
No. It edits the existing StringBuilder in place and returns this so you can chain calls.
A null String, Object, or CharSequence inserts the characters "null". A null char[] throws NullPointerException, and an uncast insert(0, null) is a compile error.
No, because each insert at index 0 shifts all existing characters to the right. For building reversed text, consider appending and calling reverse() once.