W3cubDocs

/Kotlin

zip

Platform and version requirements: JVM (1.0), JS (1.0), 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
}
Platform and version requirements: JVM (1.0), JS (1.0), 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
}

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/zip.html