String方法:分割、梦回C语言,格式化输出format

 String poem = "锄禾日当午,汗滴禾下土,谁知盘中餐,粒粒皆辛苦";
        String[] split = poem.split(",");
        System.out.println("=======悯农======");
        for (int i = 0; i < split.length; i++) {
            System.out.println(split[i]);
        }

        System.out.println("分割后的内容");
        poem = "E:\\aaa\\bbb";
        //对于转义字符换行时,需要加特殊处理‘\’
        split = poem.split("\\\\");
        for (int i = 0; i < split.length; i++) {
            System.out.println(split[i]);
        }

 String方法:分割、梦回C语言,格式化输出format_第1张图片

//format方法
        String name = "Tom";
        int age = 10;
        double score = 98.3;
        char gender = '男';
        String info =
                "我的姓名是" + name + ",年龄是" + age + ",成绩是" + score + ",性别是" + gender + "。希望大家照顾!";
        System.out.println(info);
        String formatStr = "我的名字是%s,年龄是%d,成绩是%.2f,性别是%c,希望大家照顾!";
        String info2 = String.format(formatStr,name,age,score,gender);
        System.out.println(info2);

 String方法:分割、梦回C语言,格式化输出format_第2张图片

你可能感兴趣的:(韩顺平java习题,c语言,java,服务器)