deleteCharAt removes a single character by index, and replace(start, end, text) swaps out a whole range for new text, both without ever allocating a separate String. The buffer keeps being edited in place across both calls.
StringBuilder sb = new StringBuilder("Hexlo Warld");
sb.deleteCharAt(1);
sb.replace(0, 3, "Bye");
System.out.println(sb);
Byeo Warld
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