.reverse()
Published Aug 22, 2022
Contribute to Docs
The .reverse()
method returns a modified StringBuilder
object with its character sequence rearranged in the opposite order. This is the most straightforward way to reverse a string in Java.
Syntax
myStringBuilder.reverse();
The .reverse()
method takes no arguments.
Example
The following example creates a StringBuilder
with a specified String
then uses the .reverse()
method on it:
import java.util.*;public class Example {public static void main(String[] args){StringBuilder str = new StringBuilder("Hello World!");System.out.println(str.toString());str.reverse();System.out.println(str.toString());}}
This produces the following output:
Hello World!!dlroW olleH
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.