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?
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, "", "", "")
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