Reputation: 396
Scala's splitting strings by character function discards trailing empty strings. For example "1,2,3,,,".split(',')
results in Array("1", "2", "3")
.
Is there a built-in method to split strings by character in Scala which keeps those, so that the result would be Array("1", "2", "3", "", "", "")
.
Upvotes: 0
Views: 93
Reputation: 163207
If you pass a string as the first parameter ","
then you can specify -1
as the second parameter to return the empty matches as well.
"1,2,3,,,".split(",", -1)
Output
res0: Array[String] = Array(1, 2, 3, "", "", "")
Upvotes: 2