Learner
Learner

Reputation: 1695

Split function working weird?

This is my input string and am trying to split in Scala. i find that there seems to be some fundamental difference in the way I understood split or this is not working according to my input. My input had 0,"","" as last three chars. When on split, the output stops with 0 and last two blank characters in the input string "" and "", are ignored.

This is my regular exp

val lines = "5;;ABCDEF;GHIJK;;DEC;XXXX;;;;;;YYYY;ZZZZZ;XXXXXXX ;4;YYYY;;;;;;;;;0**;;;**"    

val parsedArray = lines.map(_.split("\\;"))

Array[java.lang.String] = Array(5, "", ABCDEF, GHIJK, "", DEC, XXXX, "", "", "", "", "", YYYY, ZZZZZ, XXXXXXX, 4, YYYY, "", "", "", "", "", "", "", **"", 0)**

Can you sense something ?

Upvotes: 1

Views: 97

Answers (1)

jrudolph
jrudolph

Reputation: 8377

From http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split(java.lang.String, int):

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.

Upvotes: 3

Related Questions