Home > OS >  String splitting in Scala without discarding trailing empty strings
String splitting in Scala without discarding trailing empty strings

Time:12-18

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", "", "", "").

CodePudding user response:

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, "", "", "")
  • Related