How to easily reverse a string in Java?

Requirement:

Reverse a string in Java in One line. For example, “train” produces “niart”.

JAR Files:

Download commons-lang3-3.1.jar JAR file for the example to work.

Java Code:

The Java code program to reverse a string is provided below:
import org.apache.commons.lang3.StringUtils; // to repeat the string
public class reverseString {    
    public static void main (String[] args) {
                        String myInput="train";
                        String newRepeatedString=StringUtils.reverse(myInput);
                        //This method reverses the string
                        System.out.println("Input String: " + myInput);
                        System.out.println("Reversed : " + newRepeatedString);                  
    }
}



Example Output:


C:\Java\commons-lang3-3.1>javac -classpath .;commons-lang3-3.1.jar reverseString
.java
C:\Java\commons-lang3-3.1>java -classpath .;commons-lang3-3.1.jar reverseString


Input String: train
Reversed : niart