11

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

2
!
country=India 

Whereas with Ubuntu it gives

3

!
country=India 

Why would that be ?

azurefrog
  • 10,785
  • 7
  • 42
  • 56
user2931444
  • 131
  • 1
  • 6
  • 9
    what is Java version for both platform ? – jmj Jun 23 '15 at 17:42
  • 1
    Is the new line part of the output for Ubuntu? – Adam Evans Jun 23 '15 at 17:44
  • it would give you three parts, splitting done on the one before `!` and the one after `!`. I don't know how java takes the first empty string. – Avinash Raj Jun 23 '15 at 17:47
  • 2
    I think the behavior of split changed in a recent version of Java. – aioobe Jun 23 '15 at 17:50
  • both are using java 1.7 ... on windows - 2 ! country=India on ubuntu 3 ! country=India – user2931444 Jun 23 '15 at 18:01
  • 6
    Could you post the exact JVM versions from both environments (`java -version`)? – Dragan Bozanovic Jun 23 '15 at 18:09
  • Another recent question recently uncovered a defect in Splitter with zero-width look*ahead* patterns. Perhaps it's related? http://stackoverflow.com/questions/30941743/how-is-guava-splitter-onpattern-split-different-from-string-split – Andy Thomas Jun 23 '15 at 18:12
  • 1
    ideone gives 2 different results too. http://ideone.com/EeN5Ka (Java 7: sun-jdk-1.7.0_10 ) and https://ideone.com/e6NS0c (Java 8: sun-jdk-8u25). Definitely a JVM and not OS related issue. – matrixanomaly Jun 23 '15 at 18:20
  • I have this "build 1.7.0_76-b34" version on both win and linux its gives me 2nd output on both.I did not see any difference. – thar45 Jun 23 '15 at 18:41

1 Answers1

9

This behavior is not because of different operating systems, but likely different versions of the JVM are used.

This "behavior change" has caused bugs to be filed incorrectly for 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. This is why the additional empty string before the ! is not created (hence the length of 2 instead 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. 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). See the Java versions here. Java 8's split will provide not add the extra empty string into the array, while Java 7's split will.

This it is not because of the system being Linux or Windows, but rather the JVM version. You can double check your JVM's version with java -version

Community
  • 1
  • 1
matrixanomaly
  • 6,627
  • 2
  • 35
  • 58
  • thanks ... looks like it ... i tried on ideone myself but both 7 and 8 gave me same results (no extra line), but then i just forked your link and tried and noticed what you said... thanks for the help. – user2931444 Jun 24 '15 at 03:04
  • @user2931444 glad you solved the issue, this is an interesting behavior you've found. don't forget to accept an answer if it's the answer that is right and has helped! Welcome to SO. – matrixanomaly Jun 24 '15 at 04:45