我知道使用repeat函数我们可以重复一个字符串n次,但是如果n大于Int
的大小会怎样?
最佳答案
您可以这样做,尽管很长的字符串可能会耗尽内存
fun String.repeat(times: Long): String {
val inner = (times / Integer.MAX_VALUE).toInt()
val remainder = (times % Integer.MAX_VALUE).toInt()
return buildString {
repeat(inner) {
append(this@repeat.repeat(Integer.MAX_VALUE))
}
append(this@repeat.repeat(remainder))
}
}
关于kotlin - 当n为Long时,如何在Kotlin中将字符串重复n次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57260732/