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?
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.
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.
The java. lang. String. split(String regex) method splits this string around matches of the given regular expression.
split() will return an array.
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.
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
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