String split not returning empty results

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

    This works,

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.File;
    import java.io.IOException;
    
    public class split {
    public static void main(String[] args)
    {
        String data = null;
        try {
        BufferedReader br = new BufferedReader(new FileReader(new File("split.csv")));
        while( (data=br.readLine())!=null)
        {
            System.out.println("line:"+data);
            String[] cols = data.split(":",-1);
            System.out.println("count:"+cols.length);
            for(int x=0;x

    Here is a test file,

    a:b:c:d:e
    a:b:c:d:
    a:b:c::
    a:b::: 
    a::::
    ::::
    ::::e
    :::d:e
    ::c:d:e
    :b:c:d:e
    a:b:c:d:e
    

提交回复
热议问题