反转Java中的字符串 [英] Reverse a string in Java
本文介绍了反转Java中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将 "Hello World"
保存在名为 hi
的字符串变量中.
I have "Hello World"
kept in a String variable named hi
.
我需要打印它,但反过来了.
I need to print it, but reversed.
我该怎么做?我知道 Java 中已经内置了某种功能可以做到这一点.
How can I do this? I understand there is some kind of a function already built-in into Java that does that.
相关:用Java反转Hello World"字符串的每个单词
推荐答案
你可以使用这个:
new StringBuilder(hi).reverse().toString()
StringBuilder
是在 Java 5 中添加的.对于 Java 5 之前的版本,可以使用 StringBuffer
类代替——它具有相同的 API.
StringBuilder
was added in Java 5. For versions prior to Java 5, the StringBuffer
class can be used instead — it has the same API.
这篇关于反转Java中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文