Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java vs JavaScript split behavior

The split functionality seems to be different between Java and JavaScript.

I do not exactly need to match a certain regular expression criteria, or use lookahead based regex. My problem resides with empty matches being trailed after final matching of the split regex ( which is a simple set of character and not an expression ).

Below is an example of the output I am trying to achieve and what am I really getting.

Java

("~#~~#~~#~A~#~B~#~C~#~D~#~E~#~~#~~#~").split("~#~")

/* results with an array of length 8 */ (java.lang.String[]) [, , , A, B, C, D, E]

Javascript

 "~#~~#~~#~A~#~B~#~C~#~D~#~E~#~~#~~#~".split("~#~")
 /* results with an array of length 11 */ ["", "", "", "A", "B", "C", "D", "E", "", "", ""]

I really can't put my hands on a good explanation for this as I am working with a Java application and the split is causing me trouble, I want the same result as in JavaScript. How can I get that?

like image 825
KAD Avatar asked Jul 28 '15 08:07

KAD


People also ask

Does Javascript have a split function?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

Is there a split function in Java?

Split() String method in Java with examples. 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.

Does Java split use regex?

The java. lang. String. split(String regex) method splits this string around matches of the given regular expression.

What does split () return if the string has no match Java?

split() will return an array.


2 Answers

Javadoc on split(String regex):

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

Javadoc on split(String regex, int limit):

If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

So... split("~#~", -1) should do the trick.

like image 138
Amadan Avatar answered Oct 20 '22 04:10

Amadan


Java's split() documentation clearly mentions that the trailing empty strings will NOT be included.

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

but you can achieve it by

("~#~~#~~#~A~#~B~#~C~#~D~#~E~#~~#~~#~").split("~#~" , -1)

which is an overloaded method

like image 25
Mohit Kanwar Avatar answered Oct 20 '22 05:10

Mohit Kanwar