Amaterasu
Amaterasu

Reputation: 396

String splitting in Scala without discarding trailing empty strings

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

Answers (1)

The fourth bird
The fourth bird

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

Related Questions