7、字符串倒序

编程题目:

7.编程实现,将字符串”hello word , hello java”输出为”java hello , word hello “。

示例代码:

package program.string.exercise07;

import java.util.*;

/**
 * 7. 编程实现,将字符串"hello word , hello java"输出为"java hello , word hello ";
**/

public class StringReverse {
    public static void main(String[] args) {

        String str = "hello word , hello java";
        reverseString(str);

    }

    //倒转字符串
    private static void reverseString(String str) {

        String[] strs = str.split(" ");//以空格分割
        List<String> list = Arrays.asList(strs);//将数组转换成List集合
        Collections.reverse(list);//将集合元素顺序颠倒
        for(String s : list){
            System.out.print(s+" ");
        }

    }
}

结果显示:

这里写图片描述

猜你喜欢

转载自blog.csdn.net/jsc123581/article/details/81808829