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 ?
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"}
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.
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.
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);
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With