new StringBuilder() / builder.append() / insert()
A class for manipulating strings as mutable objects. In Java, String is immutable, so repeatedly concatenating strings with the + operator inside a loop degrades performance. StringBuilder is efficient because it rewrites an internal buffer instead of creating new objects.
Syntax
// Creates an empty StringBuilder. new StringBuilder(); // Creates a StringBuilder with an initial string. new StringBuilder(String str); // Appends a value to the end. builder.append(value); // Inserts a value at the specified position. builder.insert(int offset, value); // Deletes characters in the specified range. builder.delete(int start, int end); // Deletes the character at the specified index. builder.deleteCharAt(int index);
Method List
| Method | Description |
|---|---|
| new StringBuilder() | Creates an empty mutable string object. The initial capacity is 16 characters. |
| new StringBuilder(String str) | Creates a StringBuilder initialized with the specified string. |
| append(value) | Appends a value to the end. Accepts any type, including String, int, double, char, and boolean. Returns the builder itself, enabling method chaining. |
| insert(int offset, value) | Inserts a value at the specified index. All subsequent characters are shifted right. |
| delete(int start, int end) | Removes all characters from start up to, but not including, end. |
| deleteCharAt(int index) | Removes the single character at the specified index. |
Sample Code
// Efficiently builds a string in a loop.
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 5; i++) {
sb.append(i);
if (i < 5) sb.append(", ");
}
System.out.println(sb.toString()); // Prints "1, 2, 3, 4, 5"
// Method chaining allows multiple appends in sequence.
StringBuilder greeting = new StringBuilder()
.append("Hello")
.append(", ")
.append("Java")
.append("!");
System.out.println(greeting); // Prints "Hello, Java!"
// insert() inserts a string at a specific position.
StringBuilder sb2 = new StringBuilder("Hello World");
sb2.insert(5, ","); // Inserts a comma at index 5.
System.out.println(sb2); // Prints "Hello, World"
// delete() removes a range of characters.
StringBuilder sb3 = new StringBuilder("Java is great!");
sb3.delete(8, 14); // Removes "great".
System.out.println(sb3); // Prints "Java is !"
// deleteCharAt() removes a single character.
StringBuilder sb4 = new StringBuilder("Jaava");
sb4.deleteCharAt(2); // Removes the extra 'a'.
System.out.println(sb4); // Prints "Java"
Notes
StringBuilder is a mutable string class that efficiently handles appending, inserting, and deleting characters by rewriting an internal buffer. Avoid using the + operator on String repeatedly inside a loop, as it creates a new object on every iteration and degrades performance. Use StringBuilder for repeated concatenation.
If you need thread-safe string manipulation in a multithreaded environment, use StringBuffer, which has the same API. Note that StringBuffer synchronizes all its methods, making it slower than StringBuilder in single-threaded code.
For reversing, replacing, and converting, see 'reverse() / replace() / toString()'. For joining strings, see 'String.join()'.
If you find any errors or copyright issues, please contact us.