![]() |
VOOZH | about |
In Java, the StringBuilder class (part of the java.lang package) provides a mutable sequence of characters. Unlike the String class (which is immutable), StringBuilder allows modification of character sequences without creating new objects, making it memory-efficient and faster for frequent string operations.
Declaration:
StringBuilder sb = new StringBuilder("Initial String");
Example:
Initial StringBuilder: GeeksforGeeks After append: GeeksforGeeks is awesome!
Explanation : This example demonstrates the use of the append() method in StringBuilder to add text at the end of an existing string.
The append() method adds the given string to the end of the existing sequence without creating a new object. This makes StringBuilder efficient for concatenation operations.
It extends the AbstractStringBuilder class and implements the CharSequence and Serializable interfaces.
StringBuilder class provides multiple constructors for different use cases.
Example:
sb1: Hello sb2: This has initial capacity 50 sb3: GeeksForGeeks sb4: JavaProgramming
Explanation : This program demonstrates different constructors of the StringBuilder class and how to append data using the append() method.
The StringBuilder class provides various methods for string manipulation.
Output:
Explanation : This program demonstrates various StringBuilder methods such as append(), insert(), replace(), delete(), reverse(), substring(), and setCharAt() to perform dynamic string manipulation efficiently in Java.
| Method | Description | Example |
|---|---|---|
| append(String str) | Appends the specified string to the end of the StringBuilder. | sb.append("Geeks"); |
| insert(int offset, String) | Inserts the specified string at the given position in the StringBuilder. | sb.insert(5, " Geeks"); |
| replace(int start, int end, String) | Replaces characters in a substring with the specified string. | sb.replace(6, 11, "Geeks"); |
| delete(int start, int end) | Removes characters in the specified range. | sb.delete(5, 11); |
| reverse() | Reverses the sequence of characters in the StringBuilder. | sb.reverse(); |
| capacity() | Returns the current capacity of the StringBuilder. | int cap = sb.capacity(); |
| length() | Returns the number of characters in the StringBuilder. | int len = sb.length(); |
| charAt(int index) | Returns the character at the specified index. | char ch = sb.charAt(4); |
| setCharAt(int index, char) | Replaces the character at the specified position with a new character. | sb.setCharAt(0, 'G'); |
| substring(int start, int end) | Returns a new String that contains characters from the specified range. | String sub = sb.substring(0, 5); |
| ensureCapacity(int minimum) | Ensures the capacity of the StringBuilder is at least equal to the specified minimum. | sb.ensureCapacity(50); |
| deleteCharAt(int index) | Removes the character at the specified position. | sb.deleteCharAt(3); |
| indexOf(String str) | Returns the index of the first occurrence of the specified string. | int idx = sb.indexOf("Geeks"); |
| lastIndexOf(String str) | Returns the index of the last occurrence of the specified string. | int idx = sb.lastIndexOf("Geeks"); |
| toString() | Converts the StringBuilder object to a String. | String result = sb.toString(); |
The table below demonstrates the difference between String, StringBuilder and StringBuffer:
Features | String | StringBuilder | StringBuffer |
|---|---|---|---|
Mutability | String are immutable(creates new objects on modification) | StringBuilder are mutable(modifies in place) | StringBuffer are mutable (modifies in place) |
Thread-Safe | It is thread-safe | It is not thread-safe | It is thread-safe |
Performance | It is slow because it creates an object each time | It is faster (no object creation) | It is slower due to synchronization overhead |
Use Case | Fixed, unchanging strings | Single-threaded string manipulation | Multi-threaded string manipulation |