在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.
推荐答案
你可以使用这个:
new StringBuilder("hi").reverse().toString()
或者,对于早于JDK 1.5的版本,使用 java.util.StringBuffer
而不是 StringBuilder
- 它们具有相同的API。感谢评论员指出现在首选 StringBuilder
。
Or, for versions earlier than JDK 1.5, use java.util.StringBuffer
instead of StringBuilder
— they have the same API. Thanks commentators for pointing out that StringBuilder
is preferred nowadays.
这篇关于在Java中反转一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文