首页  >  问答  >  正文

java 输出包含逗号的字符串中的每一个字符

    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中文网2669 天前929

全部回复(3)我来回复

  • 伊谢尔伦

    伊谢尔伦2017-06-28 09:26:45

    先不管用不用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);
            }
        }

    回复
    0
  • phpcn_u1582

    phpcn_u15822017-06-28 09:26:45

    为什么不用String.split()

    回复
    0
  • 淡淡烟草味

    淡淡烟草味2017-06-28 09:26:45

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

    回复
    0
  • 取消回复