[英]Java string split gives different outputs on Windows and linux
Please see the below code -- 请看下面的代码 -
String s11 ="!country=India ";
String[] ss =s11.split("((?<=[!&|])|(?=[!&|]))");
System.out.println(ss.length);
for(String s :ss) {
System.out.println(s);
}
On Windows it gives 在Windows上,它给出了
2
!
country=India
Whereas with Ubuntu it gives 而Ubuntu给出了它
3
!
country=India
Why would that be ? 那为什么会这样?
This behavior is not because of different operating systems , but likely different versions of the JVM are used. 此行为不是因为操作系统不同 ,而是可能使用不同版本的JVM 。
This "behavior change" has caused bugs to be filed incorrectly for Java 8. 这种“行为改变”导致Java 8错误归档 。
The documentation has been updated for JDK 8, and is also discussed at length in this question, where split in Java 8 removes empty strings at the start of the result array . 该文档已针对JDK 8进行了更新,并且在此问题中也进行了详细讨论,其中Java 8中的split在结果数组的开头删除了空字符串 。 This is why the additional empty string before the
!
这就是为什么额外的空字符串之前
!
is not created (hence the length of 2 instead 3). 未创建(因此长度为2而不是3)。
Notice the difference in documentation for the split()
method in Java 7 and in Java 8 for the Pattern
class, and the string class ( Java 7 , Java 8 ) respectively. 请注意Java 7中的
split()
方法和Pattern
8中的Pattern
类以及字符串类( Java 7 , Java 8 )的文档区别。 See the original question linked for further information on this. 请参阅原始问题,以获取有关此内容的更多信息。
I have also reproduced this issue on Java 7: sun-jdk-1.7.0_10 ( ideone ) and Java 8 sun-jdk-8u25 ( ideone ). 我还在Java 7上重现了这个问题:sun-jdk-1.7.0_10( ideone )和Java 8 sun-jdk-8u25( ideone )。 See the Java versions here.
请在此处查看Java版本。 Java 8's split will provide not add the extra empty string into the array, while Java 7's split will.
Java 8的拆分将不提供额外的空字符串到数组,而Java 7的拆分将。
This it is not because of the system being Linux or Windows, but rather the JVM version. 这不是因为系统是Linux或Windows,而是JVM版本。 You can double check your JVM's version with
java -version
您可以使用
java -version
仔细检查JVM的版本
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.