Home » How to Reverse a String in Java

How to Reverse a String in Java

black text on gray background

If you need to reverse the character order of a String, you can use StringBuilder to do that.

Note that the following code prints: Java is great!

public class ReverseString {

    public static void main(String[] args) {
        var original = "!taerg si avaJ";
        var reversed = new StringBuilder(original).reverse().toString();
        System.out.println(reversed);
    }
}

If you have any questions, leave a comment or send me a message on my social media.

Leave a Reply

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