split - Splitting String in scala ignores void fields at the end -
i'm scala beginer, , i'm facing issue :
from : "abcd ; efgh ; ijkl ; ; ; ; "
i have : array["abcd ","efgh "," ijkl " , "", "" , "" ,""]
while split function returns : ["abcd ","efgh "," ijkl " ]
could please ?
thanks in advance!
this behaviour comes java method split(regex)
. if want keep trailing empty strings in returned array must use 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 string given in example works because added spaces between separators:
scala> "a , b , c , , ".split(",") res2: array[string] = array("a ", " b ", " c ", " ", " ")
Comments
Post a Comment