可以通过调用StringBuffer的reverse()方法来实现字符串的反转操作,示例如下:
public class ReverseString {
public static void main(String[] args) {
String str = "hello world";
StringBuffer stringBuffer = new StringBuffer(str);
stringBuffer.reverse();
String reversedStr = stringBuffer.toString();
System.out.println(reversedStr);
}
}
运行以上代码,输出结果为:
dlrow olleh
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:Python如何实现字符串的反转