StringBuffer 类为您提供了一个名为reverse()的方法来反转其内容。反转字符串的一种方法是将其附加到StringBuffer对象,然后将其反转并转换回String。
public class ReverseString {
public static void main(String args[]) {
String str = new String("Hello how are you");
StringBuffer sb = new StringBuffer(str);
String str2 = sb.reverse().toString();
System.out.println(str2);
}
}输出结果
uoy era woh olleH