javascala

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(',')
res1: Array[String] = Array("")

Consider that this returns empty array:

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

Please explain :)


Solution

  • 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.