在 kotlin 中解析"value=1234; some other thing"会有什么更紧凑/更干净的方法



如果我有一个字符串,其中包含一堆"key=value"形式的令牌。

例如"something variable=1234 something other thing">

考虑到键/变量的名称是已知的,并且值的类型是Int,在kotlin中解析它最紧凑、最容易的方法是什么?

(我正在考虑类似于C中scanf((的东西(

像这样简单的东西呢?

val vars = "hello, world! variable=1234 Today the sun shines variable1=5678 bright".
split(' ').
filter { it.contains('=') }.
map { it.split('=')}.
map { Pair(it[0], Integer.parseInt(it[1]))}
println(vars)

vars是成对(字符串、整数(的列表。打印:

[(variable, 1234), (variable1, 5678)]

相关内容

最新更新