12

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?

6
  • 27
    Despite the name similarities (perhaps the dumbest decision EVER), they're actually different languages. You'll find that the split in C works differently as well (by complaining about not being able to find it, mostly).
    – paxdiablo
    Jul 28, 2015 at 8:15
  • 6
    How does it even make sense that Java omits the empty strings at the end of the split, but not the prepending ones? The result Javascript gives you makes much more sense.
    – connexo
    Jul 28, 2015 at 8:16
  • 6
    @connexo: docs.oracle.com/javase/7/docs/api/java/lang/…: Trailing empty strings are therefore not included in the resulting array.
    – paxdiablo
    Jul 28, 2015 at 8:18
  • @paxdiablo, I can understand the difference, I just want the end result in JAVA to be same as Javascript
    – KAD
    Jul 28, 2015 at 8:18
  • 2
    @KAD, then you need to use the two-argument version of split with -1 as the second. See Amadan's answer.
    – paxdiablo
    Jul 28, 2015 at 8:20

3 Answers 3

31

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.

0
8

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

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.