String split not returning empty results

前端 未结 9 1518
走了就别回头了
走了就别回头了 2020-12-02 00:12

I\'m trying to use

\"value1:value2::value3\".split(\":\");

Problem is that I want it to include the blank results.

It returns:

9条回答
  •  鱼传尺愫
    2020-12-02 00:52

    Works for me.

    class t {
        public static void main(String[] _) {
            String t1 = "value1:value2::value3";
            String[] t2 = t1.split(":");
            System.out.println("t2 has "+t2.length+" elements");
            for (String tt : t2) System.out.println("\""+tt+"\"");
        }
    }
    

    gives the output

    $ java t
    t2 has 4 elements
    "value1"
    "value2"
    ""
    "value3"
    

提交回复
热议问题