Monday, August 8, 2011

Java: Reversing a String


Share at Facebook

As far I know, there is no method available under String class to reverse a provided string. You can achieve this by using the StringBuffer class. Here is an example code.


String string = "Reverse It";
string = new StringBuffer(string).reverse().toString();
System.out.println(string);


It is creating an object of StringBuffer using the string, and then doing the reverse operation using it's method. After reversing, its just converting back to string.




No comments: