StringBuilder Methods
append, insert, reverse, delete.
StringBuilder Methods is a free Java Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
More Than append
Beyond append, StringBuilder offers many methods to modify text in place: insert, delete, reverse, replace, and more. Let us explore the most useful ones.
insert
insert(index, value) places content at a specific position, shifting the rest to the right.
public class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello World");
sb.insert(5, ",");
System.out.println(sb);
}
}insert at the Start
Inserting at index 0 prepends content, something append cannot do directly.
public class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("World");
sb.insert(0, "Hello ");
System.out.println(sb);
}
}reverse
reverse() flips the entire character sequence in place. Useful for palindrome checks and similar tasks.
public class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("abcde");
sb.reverse();
System.out.println(sb);
}
}Palindrome Check
Combine reverse and toString to test whether a word reads the same backwards.
public class Main {
public static void main(String[] args) {
String word = "level";
String reversed = new StringBuilder(word).reverse().toString();
System.out.println(word.equals(reversed) ? "Palindrome" : "Not");
}
}delete
delete(start, end) removes characters from start (inclusive) to end (exclusive).
public class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello World");
sb.delete(5, 11);
System.out.println(sb);
}
}deleteCharAt
deleteCharAt(index) removes a single character at the given position.
public class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("banana");
sb.deleteCharAt(0);
System.out.println(sb);
}
}replace
replace(start, end, str) swaps a range of characters for a new string of any length.
public class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Java is hard");
sb.replace(8, 12, "fun");
System.out.println(sb);
}
}setCharAt
setCharAt(index, ch) replaces exactly one character without changing the length.
public class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("cat");
sb.setCharAt(0, 'b');
System.out.println(sb);
}
}indexOf and substring
You can search with indexOf and extract part of the builder with substring, similar to String.
public class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("name=Alice");
int eq = sb.indexOf("=");
String value = sb.substring(eq + 1);
System.out.println("Value: " + value);
}
}Chaining Modifications
Many of these methods return the builder, so you can chain edits together for compact transformations.
public class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("start");
sb.append("-end").insert(0, ">>").reverse();
System.out.println(sb);
}
}Quick Check
What does delete(start, end) remove from a StringBuilder?
Recap
You learned key StringBuilder methods:
insertadds content at a positionreverseflips the sequencedelete/deleteCharAtremove charactersreplaceandsetCharAtoverwrite contentindexOfandsubstringread parts of the builder
Frequently asked questions
Is the “StringBuilder Methods” lesson free?
Yes — the full text of “StringBuilder Methods” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “StringBuilder Methods”?
append, insert, reverse, delete. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “StringBuilder Methods” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.