Java Example: Reversing a string using StringBuilder Class

Java provides us with a StringBuilder class that allows us to reverse a string with only one statement.

Consider the following example.

package reverse;

public class Reverse {

    private static String reverseString(String str) {

        return new StringBuilder(str).reverse().toString();

    }

    public static void main(String[] args) {

        String input = "Hotel California";

        System.out.println(reverseString(input));

    }
}

rev

Series Navigation<< Java: Final method