这是一些使用 StringBuffer
反转字符串中字符的代码:
String sh = "ABCDE";
System.out.println(sh + " -> " + new StringBuffer(sh).reverse());
是否有任何类似的方法可以使用 StringBuffer
反转字符串中的单词?
输入:“我需要它”和 输出应该是:“它需要我”
最佳答案
您可以使用 StringUtils
reverseDelimited
:
Reverses a String that is delimited by a specific character. The Strings between the delimiters are not reversed. Thus java.lang.String becomes String.lang.java (if the delimiter is '.').
因此在您的情况下,我们将使用空格作为分隔符:
import org.apache.commons.lang.StringUtils;
String reversed = StringUtils.reverseDelimited(sh, ' ');
如果没有它,您可能还会找到更冗长的解决方案 here .
关于java - 使用 StringBuffer 反转字符串中单词的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35668800/