oluies oluies - 1 year ago 110
Scala Question

Why does "split" on an empty string return a non-empty array?

Split on an empty string returns an array of size 1 :

scala> "".split(',').length
res15: Int = 1

scala> "".split(',')
res17: Array[String] = Array()

Please explain :) (Since I love the way that Guava splitters are configurable and super nice!)

Answer Source

For the same reason that

",test" split ','


",test," split ','

will return an array of size 2. Everything before the first match is returned as the first element.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download