【发布时间】:2018-06-02 17:54:25
【问题描述】:
我有下面的字符串,最后有一个逗号。我想将字符串转换为列表。我正在使用下面的代码来做到这一点。
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
【问题讨论】:
-
@NiVeR :不,这里之前没有空行..请检查。
-
@NiVeR : 60.00 后有一个空行,因为它后面有一个逗号。
-
请显示实际输出
-
@NiVeR :请立即查看更新后的问题。
-
简化 - 尝试
for (String s1 : s.split(",")) { System.out.println(s1); } System.out.println("Here");- 相同的输出。