Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java string split gives different outputs on Windows and linux

Tags:

java

string

split

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 ?

like image 330
user2931444 Avatar asked Jun 23 '15 17:06

user2931444


People also ask

What is the output of split in Java?

The string split() method breaks a given string around matches of the given regular expression. After splitting against the given regular expression, this method returns a string array. Input String: 016-78967 Regular Expression: - Output : {"016", "78967"}

What does split \\ s+ do in Java?

split("\\s+") will split the string into string of array with separator as space or multiple spaces. \s+ is a regular expression for one or more spaces.

What is difference between Split and substring in Java?

Split("|"c) , you'll get an array as {"Hello", " world"} . . substring is used to get part of the string at a starting index through a specified length.

What are the examples of split () in Java?

Following are the Java example codes to demonstrate working of split () Example 1: public class GFG {. public static void main (String args []) {. String str = "geekss@for@geekss"; String [] arrOfStr = str.split ("@", 2); for (String a : arrOfStr) System.out.println (a);

How to split a string into an array in Java?

There are many ways to split a string in Java. The most common way is using the split () method which is used to split a string into an array of sub-strings and returns the new array. 1. Using String.split () The string split () method breaks a given string around matches of the given regular expression.

What is the use of split method in JavaScript?

This variant of the split method takes a regular expression as a parameter and breaks the given string around matches of this regular expression regex. Here, by default limit is 0. Returns: An array of strings is computed by splitting the given string. Throws: PatternSyntaxException – if the provided regular expression’s syntax is invalid.

How do you split a string by line in Unix?

Split String by Newline Using Regular Expressions Next, let's start by looking at the different characters used to separate lines in different operating systems. The “ ” character separates lines in Unix, Linux, and macOS. On the other hand, the “ ” character separates lines in Windows Environment.


1 Answers

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

like image 152
matrixanomaly Avatar answered Oct 07 '22 22:10

matrixanomaly