akuiper
akuiper

Reputation: 214927

String split with multiple ending delimiter

I am trying to parse a csv file where some of the lines may have missing fields, and I found this strange behavior:

scala> val s = "1,2,,,"
s: String = 1,2,,,

scala> s.split(",")
res4: Array[String] = Array(1, 2)

While I am expecting the result to be Array(1,2,"","",""). Am I missing something? If not, what is the justification of this behavior?

Upvotes: 5

Views: 676

Answers (1)

jwvh
jwvh

Reputation: 51271

That behavior was inherited from Java. Also inherited, but not fully documented, is the Java alternative split() method.

scala> val s = "1,2,,,"
s: String = 1,2,,,

scala> s.split(",", -1)
res0: Array[String] = Array(1, 2, "", "", "")

Upvotes: 3

Related Questions