将带有嵌套分隔符的字符串转换为映射



我想把这个字符串变成:

"1:a|2:b|3:c"

进入这个:

Map(1 -> "a", 2 -> "b", 3 -> "c")

我有一个有效的程序方法,但似乎应该有一种更实用的方法。

val a = [YOUR STRING]
a.split('|').map(_.split(':')).map(a => (a(0) -> a(1))).toMap
val s = "1:a|2:b|3:c"
"(\d+)\:(\w+)".r.findAllMatchIn(s).map( m => (m.group(1).toInt -> m.group(2)) ).toMap

最新更新