Use StringBuilder
to Reverse the String
In this step, we will use the reverse()
method of the StringBuilder
class to reverse the string. We will create a new StringBuilder
object and pass the original string as an argument. Then, we will call the reverse()
method on the StringBuilder
object. Finally, we will convert the StringBuilder
object back to a String using the toString()
method.
StringBuilder strBuilder = new StringBuilder(str);
strBuilder.reverse();
String reversedStr = strBuilder.toString();
We can print the reversed string using the following code.
System.out.println("Reversed String using StringBuilder: " + reversedStr);