1

ex:

val ids = "1,2,3"
var result = ids.split(",")

I need to convert string array into long array in Scala

2 Answers 2

7
val ids = "1,2,3"
val result = ids.split(",").map(_.toLong)

result: Array[Long] = Array(1, 2, 3)
Sign up to request clarification or add additional context in comments.

Comments

2
val ids = "1 ,2,  3"
val result = ids.split(',').map(_.trim.toLong)

works also with spaces between the numbers, and performs marginally better because doesn't implicitly use a regexp for the splitting part.

Comments

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.