将逗号分隔的字符串转换为最后一个逗号的列表

Convert a comma separated string to list which has a comma at the last

我有下面的字符串,最后有一个逗号。我想将字符串转换为列表。我正在使用下面的代码来做到这一点。

public class TestClass {
    public static void main(String[] args) {

        String s = "2017-07-12 23:40:00.0,153.76,16.140,60.00,,56.00,";
        String [] items = s.split(",");

        List<String> splittedString = new ArrayList<String>(Arrays.asList(items));
        for (String s1 : splittedString) {
            System.out.println(s1);
        }
        System.out.println("Here");
    }
}

此处最后一个逗号未被视为列表元素。我怎样才能更改此代码以使其正常工作。

实际输出:-

2017-07-12 23:40:00.0
153.76
16.140
60.00

56.00
Here

预期输出:-

2017-07-12 23:40:00.0
153.76
16.140
60.00

56.00

Here

将分割线改为:

String [] items = s.split(",", -1);

它应该如您所愿。这是有限制的版本。检查 reference.

这是 String#split 的预期行为。

根据 java 文档。

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.