String split not returning empty results

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

    public static void main(String[] args){
      String[] arr = "value1:value2::value3".split(":");
      for(String elm:arr){
        System.out.println("'"+elm+"',");
      }
      System.out.println(arr.length);
    }
    

    prints

    'value1',
    'value2',
    '',
    'value3',
    4
    

    Which is exactly what you want. Your mistake is somewhere else...

提交回复
热议问题