W3cubDocs

/Kotlin

joinTo

fun <T, A> Array<out T>.joinTo(
    buffer: A, 
    separator: CharSequence = ", ", 
    prefix: CharSequence = "", 
    postfix: CharSequence = "", 
    limit: Int = -1, 
    truncated: CharSequence = "...", 
    transform: (T) -> CharSequence = null
): A
fun <A> ByteArray.joinTo(
    buffer: A, 
    separator: CharSequence = ", ", 
    prefix: CharSequence = "", 
    postfix: CharSequence = "", 
    limit: Int = -1, 
    truncated: CharSequence = "...", 
    transform: (Byte) -> CharSequence = null
): A
fun <A> ShortArray.joinTo(
    buffer: A, 
    separator: CharSequence = ", ", 
    prefix: CharSequence = "", 
    postfix: CharSequence = "", 
    limit: Int = -1, 
    truncated: CharSequence = "...", 
    transform: (Short) -> CharSequence = null
): A
fun <A> IntArray.joinTo(
    buffer: A, 
    separator: CharSequence = ", ", 
    prefix: CharSequence = "", 
    postfix: CharSequence = "", 
    limit: Int = -1, 
    truncated: CharSequence = "...", 
    transform: (Int) -> CharSequence = null
): A
fun <A> LongArray.joinTo(
    buffer: A, 
    separator: CharSequence = ", ", 
    prefix: CharSequence = "", 
    postfix: CharSequence = "", 
    limit: Int = -1, 
    truncated: CharSequence = "...", 
    transform: (Long) -> CharSequence = null
): A
fun <A> FloatArray.joinTo(
    buffer: A, 
    separator: CharSequence = ", ", 
    prefix: CharSequence = "", 
    postfix: CharSequence = "", 
    limit: Int = -1, 
    truncated: CharSequence = "...", 
    transform: (Float) -> CharSequence = null
): A
fun <A> DoubleArray.joinTo(
    buffer: A, 
    separator: CharSequence = ", ", 
    prefix: CharSequence = "", 
    postfix: CharSequence = "", 
    limit: Int = -1, 
    truncated: CharSequence = "...", 
    transform: (Double) -> CharSequence = null
): A
fun <A> BooleanArray.joinTo(
    buffer: A, 
    separator: CharSequence = ", ", 
    prefix: CharSequence = "", 
    postfix: CharSequence = "", 
    limit: Int = -1, 
    truncated: CharSequence = "...", 
    transform: (Boolean) -> CharSequence = null
): A
fun <A> CharArray.joinTo(
    buffer: A, 
    separator: CharSequence = ", ", 
    prefix: CharSequence = "", 
    postfix: CharSequence = "", 
    limit: Int = -1, 
    truncated: CharSequence = "...", 
    transform: (Char) -> CharSequence = null
): A
fun <T, A> Iterable<T>.joinTo(
    buffer: A, 
    separator: CharSequence = ", ", 
    prefix: CharSequence = "", 
    postfix: CharSequence = "", 
    limit: Int = -1, 
    truncated: CharSequence = "...", 
    transform: (T) -> CharSequence = null
): A

Appends the string from all the elements separated using separator and using the given prefix and postfix if supplied.

If the collection could be huge, you can specify a non-negative value of limit, in which case only the first limit elements will be appended, followed by the truncated string (which defaults to "...").

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val sb = StringBuilder("An existing string and a list: ")
val numbers = listOf(1, 2, 3)
println(numbers.joinTo(sb, prefix = "[", postfix = "]").toString()) // An existing string and a list: [1, 2, 3]

val lotOfNumbers: Iterable<Int> = 1..100
val firstNumbers = StringBuilder("First five numbers: ")
println(lotOfNumbers.joinTo(firstNumbers, limit = 5).toString()) // First five numbers: 1, 2, 3, 4, 5, ...
//sampleEnd
}

© 2010–2018 JetBrains s.r.o.
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/join-to.html