dcsimg

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);
buffer.reverse();
s = buffer.toString();
System.out.println(s);
// displays: dlroW olleH