gpt4 book ai didi

string - 在kotlin中合并两个字符串

转载 作者:行者123 更新时间:2023-12-02 07:03:36 25 4
gpt4 key购买 nike

我有两个字符串

val a = "abc"
val b = "xyz"

我想合并它并需要如下输出

axbycz

我将两个字符串添加到数组列表中,然后对其进行平面映射

val c = listOf(a, b)

val d = c.flatMap {
it.toList()
}

但没有得到想要的结果

最佳答案

使用zip函数。它创建具有“相邻”字母的对的列表。然后,您可以将 joinToString 与转换器结合使用来创建最终结果。

a.zip(b) // Returns the list [(a, x), (b, y), (c, z)]
.joinToString("") { (a, b) -> "$a$b" } // Joins the list back to a string with no separator

关于string - 在kotlin中合并两个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54533800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com