StringBuilder reverse() in Java with Examples
Last Updated :
11 Dec, 2022
The reverse() method of StringBuilder is used to reverse the characters in the StringBuilder. The method helps to this character sequence to be replaced by the reverse of the sequence. Syntax:
public java.lang.AbstractStringBuilder reverse()
Returns: This method returns StringBuilder object after reversing the characters. Below programs illustrate the java.lang.StringBuilder.reverse() method: Example 1:
Java
class GFG {
public static void main(String[] args)
{
StringBuilder str
= new StringBuilder("WelcomeGeeks");
System.out.println("String = "
+ str.toString());
StringBuilder reverseStr = str.reverse();
System.out.println("Reverse String = "
+ reverseStr.toString());
}
}
|
Output:
String = WelcomeGeeks
Reverse String = skeeGemocleW
Example 2:
Java
class GFG {
public static void main(String[] args)
{
StringBuilder str
= new StringBuilder("AAAABBBCCCC");
System.out.println("String = "
+ str.toString());
StringBuilder reverseStr = str.reverse();
System.out.println("Reverse String = "
+ reverseStr.toString());
}
}
|
Output:
String = AAAABBBCCCC
Reverse String = CCCCBBBAAAA
References: https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#reverse()
Share your thoughts in the comments
Please Login to comment...