builder.reverse() / replace() / toString()
Methods for reversing the content of a StringBuilder, replacing a specified range with another string, and converting the result to a String. These are typically used as finishing operations to complete mutable string manipulation.
Syntax
// Reverses the string. builder.reverse(); // Replaces the specified range with another string. builder.replace(int start, int end, String str); // Returns a substring of the specified range. builder.substring(int start); builder.substring(int start, int end); // Converts the StringBuilder to a String. builder.toString(); // Returns the current number of characters. builder.length();
Method List
| Method | Description |
|---|---|
| reverse() | Reverses the character sequence in the builder and returns itself. Surrogate pairs are reversed correctly. |
| replace(int start, int end, String str) | Replaces the characters from start up to (but not including) end with str. Returns itself. |
| substring(int start) | Returns a String containing the characters from start to the end. The builder itself is not modified. |
| substring(int start, int end) | Returns a String containing the characters from start up to (but not including) end. |
| toString() | Converts the builder's content to a String and returns it. Call this when you need to extract the final string value. |
| length() | Returns the current number of characters in the builder. |
Sample Code
// Use reverse() to reverse a string.
StringBuilder sb = new StringBuilder("Hello");
sb.reverse();
System.out.println(sb); // Prints "olleH"
// Check whether a word is a palindrome.
String word = "racecar";
String reversed = new StringBuilder(word).reverse().toString();
System.out.println(word.equals(reversed)); // Prints "true"
// Use replace() to replace a range of characters.
StringBuilder sb2 = new StringBuilder("Java is fun!");
sb2.replace(8, 11, "great"); // Replaces "fun" with "great"
System.out.println(sb2); // Prints "Java is great!"
// Use substring() to extract a substring (the builder is not modified).
StringBuilder sb3 = new StringBuilder("Hello, Java!");
System.out.println(sb3.substring(7)); // Prints "Java!"
System.out.println(sb3.substring(7, 11)); // Prints "Java"
// Use toString() to get the final String.
StringBuilder sb4 = new StringBuilder();
sb4.append("Result: ").append(42);
String result = sb4.toString();
System.out.println(result); // Prints "Result: 42"
// Use length() to check the current character count.
System.out.println(sb4.length()); // Prints "10"
Notes
reverse() is a convenient method that reverses a string in a single line — useful for algorithm problems and palindrome checks. Because it returns itself, you can use method chaining.
replace() differs from String's replaceAll() in that it replaces characters by index range rather than by pattern. Regular expressions are not supported. If the specified range exceeds the length of the string, a StringIndexOutOfBoundsException is thrown, so use length() to verify the length beforehand.
For appending, inserting, and deleting, see append() / insert() / delete(). For broader string manipulation, see String's replace() / replaceAll().
If you find any errors or copyright issues, please contact us.