本文介绍了在java中反转句子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的程序中插入一个字符串
I m inserting a string in my program
String str = " I live in India";
怎样才能获得相反的字符串
how can in get reversed string of that like
String str ="India in live I"
这是我在采访中的一个面试问题。请任何人在这个问题上帮助我
this is an interview question in my interview. Please any one can help me out in this question
推荐答案
拆分它然后将其添加到新的字符串
以相反的顺序。
Split it and then add it to a new String
in reverse order.
String s = " I live in India";
String[] split = s.split(" ");
String result = "";
for (int i = split.length - 1; i >= 0; i--) {
result += (split[i] + " ");
}
System.out.println(result.trim());
这打印:
India in live I
这篇关于在java中反转句子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!