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 笔记的主要内容,如果未能解决你的问题,请参考以下文章