How do I reverse the contents of a String?

John Zukowski

The StringBuffer class supports reversing the sequence of characters which it contains. You would need to convert your String to a StringBuffer, reverse it, and then get the StringBuffer back as a String.

String s = "Hello World";
StringBuffer buffer = new StringBuffer(s);
s = buffer.toString();
// displays: dlroW olleH
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact