Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 444
Psidom Avatar asked Oct 17 '22 13:10

Psidom


1 Answers

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, "", "", "")
like image 132
jwvh Avatar answered Oct 21 '22 05:10

jwvh