0

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

0

1 Answer 1

2

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, "", "", "")
Sign up to request clarification or add additional context in comments.

1 Comment

ok, actually after testing: if escaped properly this function is not slower than splitting by character

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.