leeolevis
leeolevis

Reputation: 13

How do i convert string array into long array in Scala

ex:

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

I need to convert string array into long array in Scala

Upvotes: 1

Views: 1370

Answers (2)

Erik Kaplun
Erik Kaplun

Reputation: 38207

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.

Upvotes: 2

Leo
Leo

Reputation: 38190

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

result: Array[Long] = Array(1, 2, 3)

Upvotes: 7

Related Questions