给定一个字符串,逐个翻转字符串中的每个单词

时间:2022-04-11 17:35:37

示例:

输入:
"the sky is blue",

输出:
"blue is sky the".

 public  String reverseWords(String s) {
        StringBuilder sb = new StringBuilder();
        if(s == null || "".equals(s.trim())){
            return "";
        }else{
            String [] strs = s.trim().split("\s ");
            int len = strs.length-1;
            for(int i=len;i>=0;i--){
                if(i == 0){
                    sb.append(strs[i]);
                }else{
                    sb.append(strs[i] " ");
                }
            }
            return sb.toString();
        }
    }

trim()去除字符串两头空格,split()的用法