zip

Common
JVM
JS
Native
1.0
infix fun CharSequence.zip(
    other: CharSequence
): List<Pair<Char, Char>>

Returns a list of pairs built from the characters of this and the other char sequences with the same index The returned list has length of the shortest char sequence.

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
}
Common
JVM
JS
Native
1.0
inline fun <V> CharSequence.zip(
    other: CharSequence,
    transform: (a: Char, b: Char) -> V
): List<V>

Returns a list of values built from the characters of this and the other char sequences with the same index using the provided transform function applied to each pair of characters. The returned list has length of the shortest char sequence.

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
}