Kotlin 方法链接以处理列表中的字符串

Kotlin method chaining to process strings in a list

我有一个字符串列表,它是拆分字符串后得到的。我需要从列表中的字符串中删除周围的引号。使用方法链我怎样才能做到这一点?我尝试了下面的方法,但没有 work.Says 类型干扰失败。

val splitCountries: List<String> = countries.split(",").forEach{it -> it.removeSurrounding("\"")}

forEach doesn't return the value you generate in it, it's really just a replacement for a for loop that performs the given action. What you need here is map:

val splitCountries: List<String> = countries.split(",").map { it.removeSurrounding("\"") }

此外,lambda 中的单个参数被隐式命名为 it,如果您想更改它,您只需显式命名它。