String split not returning empty results

前端 未结 9 1525
走了就别回头了
走了就别回头了 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:43

    Use a negative limit in your split statement:

    String str = "val1:val2::val3";
    String[] st = str.split(":", -1);
    for (int i = 0; i< st.length; i++)
        System.out.println(st[i]);
    

    Results:

    val1
    val2
    
    val3
    

提交回复
热议问题