Reverse a String in Java Using StringBuilder

This example shows how to reverse a String in Java using StringBuilder.reverse() method with an example.

Reverse a String in Java Using StringBuilder


package net.javaguides.corejava.string;

/**
 * 
 * @author sourcecodeexamples.net
 *
 */
public class ReverseWithStringBuilderBuiltinMethod {
    public static void main(String[] args) {
        ReverseWithStringBuilderBuiltinMethod builtinMethod = new ReverseWithStringBuilderBuiltinMethod();
        builtinMethod.reverseWithStringBuilderBuiltinMethod("sourcecodeexamples");
    }

    public String reverseWithStringBuilderBuiltinMethod(String string) {
        final StringBuilder builder = new StringBuilder(string);
        display(string, builder.reverse().toString());
        return builder.reverse().toString();
    }

    private void display(String input, String output) {
        System.out.println(" input string :: " + input);
        System.out.println(" output string :: " + output);
    }
}
Output:
 input string :: sourcecodeexamples
 output string :: selpmaxeedocecruos

Comments