zip所在位置是kotlin.text.zip,其相关用法介绍如下。

用法一

infix fun CharSequence.zip(
    other: CharSequence
): List<Pair<Char, Char>>

返回由具有相同索引的thisother 字符序列的字符构建的对列表。返回的列表具有最短字符序列的长度。

例子:

import java.util.Locale
import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val stringA = "abcd"
val stringB = "zyx"
println(stringA zip stringB) // [(a, z), (b, y), (c, x)]
//sampleEnd
}

输出:

[(a, z), (b, y), (c, x)]

用法二

inline fun <V> CharSequence.zip(
    other: CharSequence, 
    transform: (a: Char, b: Char) -> V
): List<V>

使用提供的适用于每对字符的 transform 函数,返回由 this 和具有相同索引的 other 字符序列的字符构建的值列表。返回的列表具有最短字符序列的长度。

例子:

import java.util.Locale
import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val stringA = "abcd"
val stringB = "zyx"
val result = stringA.zip(stringB) { a, b -> "$a$b" }
println(result) // [az, by, cx]
//sampleEnd
}

输出:

[az, by, cx]