Submitting the form below will ensure a prompt response from us.
Reversing a string using StringBuilder in Java is simple and efficient because StringBuilder provides a built-in reverse() method that allows you to reverse the string in a single step.
First, create a StringBuilder object and pass the string you want to reverse to it.
StringBuilder sb = new StringBuilder("Hello");
Call the reverse() method on the StringBuilder object.
sb.reverse();
If you need the reversed string as a String object, use the toString() method.
String reversedString = sb.toString();
System.out.println(reversedString); // Output: "olleH"
public class ReverseStringExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Java");
sb.reverse(); S
ystem.out.println(sb.toString()); // Output: "avaJ"
}
}
Submitting the form below will ensure a prompt response from us.