简体   繁体   English

Java String拆分无法正常工作

[英]Java String split is not working

Java Experts , Java专家,

Please look into the below split command code and let me know why last two nulls are not captured. 请查看下面的拆分命令代码,让我知道为什么不捕获最后两个空值。

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);
}

The result iam getting 结果我得到了

length -7
value1
valueO1
value
value
value
value0.0000
value0.0000

surprisingly the length is 7 where as it should be 9 and also as you can see values after 0.0000 ie two last nulls are not coming . 令人惊讶的是,长度为7,因为它应该是9,也可以看到0.0000之后的值,即两个最后的空值不会出现。 Lets say now if i change the string test "1,O1,,,,0.0000,0.0000,0,0" 让我们说现在,如果我改变字符串测试“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);
}

I am getting correctly 我说得对

length -9
value1
valueO1
value
value
value
value0.0000
value0.0000
value0
value0

I don't think iam doing wrong . 我不认为我做错了。 Is it a bug ? 这是一个错误吗? JAVA Version - jdk1.6.0_31 JAVA版本 - jdk1.6.0_31

It behaves as specified in the javadoc : 它的行为与javadoc中指定的相同

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. 此方法的作用就像通过调用具有给定表达式和limit参数为零的双参数split方法一样。 Trailing empty strings are therefore not included in the resulting array. 因此,结尾的空字符串不包含在结果数组中。

If you want to keep the trailing blank strings, you can use the 2 argument split method with a negative limit: 如果要保留尾随空白字符串,可以使用带有负限制的2参数split方法

String[] splittest = test.split(",", -1);

If the limit is non-positive then the pattern will be applied as many times as possible and the array can have any length. 如果限制是非正数,那么模式将被应用尽可能多的次数,并且数组可以具有任何长度。

split silently discards trailing separators, as specified in the Javadoc. split静默地丢弃尾随分隔符,如Javadoc中所指定的那样。

In general, the behavior of split is kind of weird . 一般来说, split的行为有点奇怪 Consider using Guava's Splitter instead, which has somewhat more predictable and customizable behavior. 考虑使用Guava的 Splitter ,它具有更多可预测和可定制的行为。 (Disclosure: I contribute to Guava.) (披露:我向番石榴捐款。)

Splitter.on(',').split("1,O1,,,,0.0000,0.0000,,");
// returns [1, O1, , , , 0.0000, 0.0000, , ]
Splitter.on(',').omitEmptyStrings()
  .split("1,O1,,,,0.0000,0.0000,,");
// returns [1, O1, 0.0000, 0.0000]

As mentioned above, test.split(","); 如上所述, test.split(","); will ignore trailing blank strings. 将忽略尾随空白字符串。 You could use the two parameter method with a large second argument. 您可以将两个参数方法与大的第二个参数一起使用。 However, the API also states 但是,API也说明了这一点

If n is non-positive then the pattern will be applied as many times
as possible and the array can have any length.

where n is the second argument. 其中n是第二个参数。 So if you want all the trailing strings, I would recommend 所以如果你想要所有尾随字符串,我会建议

test.split(",", -1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM