Reputation: 13
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
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
Reputation: 38190
val ids = "1,2,3"
val result = ids.split(",").map(_.toLong)
result: Array[Long] = Array(1, 2, 3)
Upvotes: 7