Reputation: 31
I'm a scala beginer, and I'm facing this issue :
From : "abcd ; efgh ; ijkl ; ; ; ; "
I would like to have : Array["abcd ","efgh "," ijkl " , "", "" , "" ,""]
While the split function returns : ["abcd ","efgh "," ijkl " ]
Could Someone help please ?
Thanks in advance!
Upvotes: 0
Views: 85
Reputation: 1828
This behaviour comes from the Java method split(regex)
. If you want to keep the trailing empty strings in your returned array you must use the overloaded method split(regex, limit)
:
scala> "a,b,c,,".split(",")
res0: Array[String] = Array(a, b, c)
scala> "a,b,c,,".split(",", -1)
res1: Array[String] = Array(a, b, c, "", "")
Note that the string given in your example actually works because you added spaces between the separators:
scala> "a , b , c , , ".split(",")
res2: Array[String] = Array("a ", " b ", " c ", " ", " ")
Upvotes: 3