oluies oluies - 3 months ago 9
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

For the same reason that

",test" split ','

and

",test," split ','

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

Comments