Kotlin joinToString 笔记

Posted aikongmeng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Kotlin joinToString 笔记相关的知识,希望对你有一定的参考价值。

添加链接描述
官方demo

//joinToString  查看源码
public fun IntArray.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Int) -> CharSequence)? = null): String 
    return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString()

Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied.
撇脚翻译:
使用[separator]和给定的[prefix]和[postfix](将所有元素分隔开,创建一个字符串。

import kotlin.test.*

fun main(args: Array<String>) 
    val numbers = listOf(1, 2, 3, 4, 5, 6)
    println(numbers.joinToString()) // 1, 2, 3, 4, 5, 6
    println(numbers.joinToString(prefix = "[", postfix = "]")) // [1, 2, 3, 4, 5, 6]
    println(numbers.joinToString(prefix = "<", postfix = ">", separator = "•")) // <1•2•3•4•5•6>

    val chars = charArrayOf('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q')
    println(chars.joinToString(limit = 5, truncated = "...!")  it.uppercaseChar().toString() ) // A, B, C, D, E, ...!

实例:

enum class ProtocolState 
    WAITING ,
    TALKING

//打印枚举类所有常量
inline fun <reified T : Enum<T>> printAllValues() 
    print(enumValues<T>().joinToString  it.name )

fun main() 
   printAllValues<ProtocolState>()

以上是关于Kotlin joinToString 笔记的主要内容,如果未能解决你的问题,请参考以下文章

Kotlin joinToString 笔记

Kotlin joinToString 笔记

Kotlin 集合List SetMap操作汇总

错误记录Kotlin 编译报错 ( Not nullable value required to call an ‘iterator()‘ method on for-loop range )(代码片

Kotlin 延迟初始化和密封类[第一行代码 Kotlin 学习笔记]

Kotlin 扩展函数和运算符重载[第一行代码 Kotlin 学习笔记]