insert(index, text) shifts the existing characters aside and drops new text in at a position, all within the same buffer. It avoids slicing the string into pieces and concatenating them back together.
StringBuilder sb = new StringBuilder("Hello world");
sb.insert(5, ",");
sb.insert(sb.length(), "!");
System.out.println(sb);
Hello, world!
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-09-27