StringBuilder reverse() In Java

StringBuilder reverse() In Java | The reverse() method in the Java StringBuilder class helps us to reverse the StringBuilder object content. The String class doesn’t contain any method to reverse the string object, but with the help of StringBuilder class we can reverse the string object also, we will see them through example.

The syntax for the reverse method is as follows:-
public StringBuilder reverse()
This method does not take any parameters.
Return Type: StringBuilder.
Returns: The reversed StringBuilder.

Example-1:
StringBuilder: “Hi”
After Reverse: “iH”

Example-2:
StringBuilder: “Phone”
After Reverse: “enohP”

Java StringBuilder reverse() Method Example-1

In the below program we are passing the string to the StringBuilder class constructor i.e. we are creating the object for the StringBuilder class. We called the reverse() method on the given StringBuilder object and it returns a new StringBuilder object which is assigned to the string1 variable.

public class Main {
   public static void main(String[] args) {
      StringBuilder sb = new StringBuilder("Good Morning");
      System.out.println("StringBuilder = " + sb);

      StringBuilder sb1 = sb.reverse();
      System.out.println("Reverse = " + sb1.toString());
   }
}

Output:-

StringBuilder = Good Morning
Reverse = gninroM dooG

StringBuilder Reverse In Java Example-2

public class Main {
   public static void main(String[] args) {
      StringBuilder sb = new StringBuilder("REVERSE");
      System.out.println("StringBuilder = " + sb);

      StringBuilder sb1 = sb.reverse();
      System.out.println("Reverse = " + sb1);
   }
}

Output:-

StringBuilder = REVERSE
Reverse = ESREVER

StringBuilder Reverse In Java Example-3

public class Main {
   public static void main(String[] args) {

      StringBuilder strb1 = new StringBuilder("reverse");
      System.out.println("StringBuilder1 = " + strb1);
      System.out.println("Reverse1 = " + strb1.reverse());

      StringBuilder strb2 = new StringBuilder("789632");
      System.out.println("StringBuilder2 = " + strb2);
      System.out.println("Reverse2 = " + strb2.reverse());
   }
}

Output:-

StringBuilder1 = reverse
StringBuilder2 = 789632
Reverse1 = esrever
Reverse2 = 236987

Reverse a String In Java Using StringBuilder Example-1

Let us see how to reverse a string in Java using the StringBuilder class. The string class in Java doesn’t contain any built-in method to reverse the string object but we can reverse the string object using the StringBuilder class.

We have to convert String to StringBuilder and then call the reverse() method on the StringBuilder object which results reversed StringBuilder object. Later convert StringBuilder to a String object. Finally, we will have reversed string values in String format.

public class Main {
   public static void main(String[] args) {
      String string = "Programming Language";
      System.out.println("String = " + string);

      // create StringBuilder from String
      StringBuilder strb = new StringBuilder(string);
      System.out.println("StringBuilder = " + strb);

      // reverse StringBuilder
      StringBuilder sb1 = strb.reverse();

      // convert StringBuilder to String
      String string1 = sb1.toString();

      System.out.println("Reverse string = " + string1);
   }
}

Output:-

String = Programming Language
StringBuilder = Programming Language
Reverse string = egaugnaL gnimmargorP

Reverse a String In Java Using StringBuilder Example-2

We can do all the above operations (converting String to StringBuilder, reversing the StringBuilder object, converting StringBuilder back to String) in a single line. The below program demonstrate it:-

public class Main {
   public static void main(String[] args) {
      String string = "Hello World";
      System.out.println("String = " + string);

      String reverseString = new StringBuilder(string).reverse().toString();
      System.out.println("Reverse string = " + reverseString);
   }
}

Output:-

String = Hello World
Reverse string = dlroW olleH

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *