Search code examples
arraysstringintkotlin

Kotlin String to Int array


I´m looking for the most efficient way to convert an String like

"[1,2,3,4,5]"

to an array of Int [1,2,3,4,5] in Kotlin


Solution

  • Fortunately I've been able to make it work, so I'll leave it here for future reference

    val result = "[1,2,3,4,5]".removeSurrounding("[", "]").split(",").map { it.toInt() }
    

    Many thanks to all!