java 输出包含逗号的字符串中的每一个字符
PHP中文网
PHP中文网 2017-06-28 09:24:45
0
3
919
    public void evaluateCourse() {
//        Res res = new Res();
//        Connection con = null;
        String total = "1,2,33,53";
        for(int i = 0;i < total.length() ; i ++){
            if(total.charAt(i) != ','){
                System.out.println(total.charAt(i));
            }
        }
    }

输出结果是

我想要的结果是: 1

            2
            33
            53
PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(3)
伊谢尔伦

先不管用不用String.split()

    public void evaluateCourse() {
//        Res res = new Res();
//        Connection con = null;
        String total = "1,2,33,53";
        String temp = "";
        for(int i = 0;i < total.length() ; i ++){
            temp += total.charAt(i);
            if(total.charAt(i) == ','){
                System.out.println(temp);
                temp = "";
            }
        }
    }

再看看用split的

public void evaluateCourse() {
//        Res res = new Res();
//        Connection con = null;
        String total = "1,2,33,53";
        String[] temp = aa.split(",");
        for(String s : temp) {
            System.out.println(s);
        }
    }
phpcn_u1582

为什么不用String.split()

淡淡烟草味

Stream.of(total.split(",")).forEach(System.out::println);

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!