StringBuilder reverse() in Java with Examples (original) (raw)
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()
Similar Reads
- StringBuilder replace() in Java with Examples The replace(int start, int end, String str) method of StringBuilder class is used to replace the characters in a substring of this sequence with characters in the specified String. The substring begins at the specified index start and extends to the character at index end - 1 or to the end of the se 3 min read
- StringBuffer reverse() Method in Java with Examples The Java.lang.StringBuffer.reverse() is an inbuilt method that is used to reverse the characters in the StringBuffer. The method causes this character sequence to be replaced by the reverse of the sequence. Syntax: public StringBuffer reverse() Parameters: NA Return Value: The method returns the Str 2 min read
- StringBuilder setCharAt() in Java with Examples The setCharAt(int index, char ch) method of StringBuilder class is used to set the character at the position index passed as ch. This method changes the old sequence to represents a new sequence which is identical to old sequence only difference is a new character ch is present at position index. Th 2 min read
- StringBuilder setLength() in Java with Examples The setLength(int newLength) method of StringBuilder is used to set the length of the character sequence equal to newLength.For every index k greater than 0 and less than newLength. If the newLength passed as argument is less than the old length, the old length is changed to the newLength.If the new 3 min read
- StringBuilder delete() in Java with Examples The delete(int start, int end) method of StringBuilder class removes the characters starting from index start to index end-1 from String contained by StringBuilder. This method takes two indexes as a parameter first start represents index of the first character and endIndex represents index after th 3 min read
- StringBuilder charAt() in Java with Examples The charAt(int index) method of StringBuilder Class is used to return the character at the specified index of String contained by StringBuilder Object. The index value should lie between 0 and length()-1. Syntax: public char charAt(int index) Parameters: This method accepts one int type parameter in 3 min read
- StringBuilder getChars() in Java with Examples The getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) method of StringBuilder class copies the characters starting at the given index:srcBegin to index:srcEnd-1 from String contained by StringBuilder into an array of char passed as parameter to function. The characters are copied from Str 3 min read
- StringBuilder toString() method in Java with Examples The toString() method of the StringBuilder class is the inbuilt method used to return a string representing the data contained by StringBuilder Object. A new String object is created and initialized to get the character sequence from this StringBuilder object and then String is returned by toString( 3 min read
- StringBuilder subSequence() in Java with Examples The subSequence(int start, int end) method of StringBuilder class is the inbuilt method used to return a subsequence of characters lie between index start and end-1 of this sequence. The subsequence starts with the char value at the index start and ends with the char value at (end-1). The length of 3 min read
- StringBuilder substring() method in Java with examples In StringBuilder class, there are two types of substring method depending upon the parameters passed to it. substring(int start) The substring(int start) method of StringBuilder class is the inbuilt method used to return a substring start from index start and extends to end of this sequence. The str 3 min read