Java 专家,
请查看下面的拆分命令代码,让我知道为什么最后两个空值没有被捕获。
String test = "1,O1,,,,0.0000,0.0000,,";
String[] splittest = test.split(",");
System.out.println("length -"+splittest.length);
for (String string : splittest) {
System.out.println("value"+string);
}
我得到的结果
length -7
value1
valueO1
value
value
value
value0.0000
value0.0000
令人惊讶的是,长度是 7,它应该是 9,而且你可以看到 0.0000 之后的值,即最后两个空值不会出现。现在让我们说如果我更改字符串测试“1,O1,,,,0.0000,0.0000,0,0”
String test = "1,O1,,,,0.0000,0.0000,0,0";
String[] splittest = test.split(",");
System.out.println("length -"+splittest.length);
for (String string : splittest) {
System.out.println("value"+string);
}
我得到正确
length -9
value1
valueO1
value
value
value
value0.0000
value0.0000
value0
value0
我不认为我做错了。这是一个错误吗?JAVA版本-jdk1.6.0_31