public String ReverseSentence(String str) { if(str==null||str.equals("")) return ""; String res = ""; String[] strs = str.split(" "); if(strs.length==0){//说明全是空格 for(int j = 0;j<str.length();j++){ res += " "; } return res; } //倒着写 for(int i=strs.length-1;i>=0;i--){ res +=strs[i]+" "; } //多加了一个空格所以去除掉 res = res.substring(0,res.length()-1); return res; }