Builder: deleteCharAt and replace edit a range in place

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.

Code
StringBuilder sb = new StringBuilder("Hexlo Warld");
sb.deleteCharAt(1);
sb.replace(0, 3, "Bye");
System.out.println(sb);
Output
Byeo Warld
Advertisement
More in JAVA

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

© Java Coding Hub · About · Contact · Privacy · Terms