Java StringBuilder
A StringBuilder is a Java object used to build and change text. Unlike String, a StringBuilder is mutable, which means its contents can be edited after it is created.
Use StringBuilder when your program needs to add, remove, or update text many times.
Why Use StringBuilder?
A regular String cannot be changed directly. When you join strings with +, Java creates a new string value. That is fine for short messages, but repeated string joining can create many temporary objects.
StringBuilder lets you keep one growing text buffer and modify it with methods such as append(), insert(), replace(), and delete().
Create And Append Text
Create a StringBuilder with new StringBuilder(). You can start empty or pass in starting text.
public class Main {
public static void main(String[] args) {
StringBuilder message = new StringBuilder();
message.append("Java");
message.append(" ");
message.append("is");
message.append(" ");
message.append("useful");
message.append(".");
System.out.println(message);
System.out.println("Length: " + message.length());
}
}
Output:
Java is useful.
Length: 15
The append() method adds text to the end. It can also append numbers, characters, booleans, and other values by converting them to text.
Editing Text
StringBuilder has methods for changing text in place. Indexes start at 0, just like string indexes.
| Method | What it does |
|---|---|
append(value) |
Adds a value to the end. |
insert(index, value) |
Adds a value at a specific index. |
replace(start, end, value) |
Replaces characters from start up to, but not including, end. |
delete(start, end) |
Removes characters from start up to, but not including, end. |
reverse() |
Reverses the characters. |
toString() |
Returns the current contents as a String. |
public class Main {
public static void main(String[] args) {
StringBuilder text = new StringBuilder("Java code");
text.insert(5, "beginner ");
System.out.println(text);
text.replace(5, 13, "simple");
System.out.println(text);
text.delete(11, 16);
System.out.println(text);
String result = text.toString();
System.out.println("Final: " + result);
}
}
Output:
Java beginner code
Java simple code
Java simple
Final: Java simple
Method Chaining
Many StringBuilder methods return the same builder object. This lets you chain calls together.
public class Main {
public static void main(String[] args) {
StringBuilder list = new StringBuilder();
list.append("Tasks: ")
.append("read")
.append(", practice")
.append(", review");
System.out.println(list.toString());
}
}
Output:
Tasks: read, practice, review
StringBuilder Vs String
Use String for ordinary text, especially when the value will not change much. Use StringBuilder when you are building text piece by piece, such as making a report, creating a long message, or processing text inside a loop.
When you need a regular string again, call toString(). This is useful when passing the result to code that expects a String.
Common Mistakes
- Remember that
StringBuilderchanges itself. You usually do not need to assign the result ofappend()back to the same variable. - Use valid indexes with
insert(),replace(), anddelete(). - Use
toString()when a method specifically needs aString.
Takeaway: use StringBuilder when you need changeable text that can be built and edited efficiently.
