In this example we will show how to reverse a given string in Java.
Source Code
package com.beginner.examples;
public class StringReverse {
public static void main(String[] args) {
//Create String.
String string = "Hello World";
System.out.println("Before reversing , the String is : " + string);
/*
* To reverse a given string use the method reverse() of java StringBuffer class.
*/
string = new StringBuffer(string).reverse().toString();
System.out.println("After reversing , the String is " + string);
}
}
Output:
Before reversing , the String is : Hello World
After reversing , the String is dlroW olleH