Kotlin:如何通过将字符重复给定次数来创建字符串
Kotlin: How to create a string by repeating a character a given number of times
我正在尝试在标题下方打印一行。这个想法是该行与标题的长度相同。我尝试了几种方法,我认为这是最接近的,但它给我的结果不正确。
fun main() {
val chapters = arrayOf("Basic syntax", "Idioms", "Kotlin by example", "Coding conventions")
for (numCharpter in chapters.indices){
// Print the name of the chapter
val title = "Charpter ${numCharpter + 1}: ${chapters[numCharpter]}"
val arrayDash = Array(title.length) {'='}
val stringDash = arrayDash.toString()
println("$title\n$stringDash\n")
// the rest of the code
}
}
我想要的输出是:
Charpter 1: Basic syntax
========================
Charpter 2: Idioms
==================
Charpter 3: Kotlin by example
=============================
Charpter 4: Coding conventions
==============================
我得到的输出是:
Charpter 1: Basic syntax
[Ljava.lang.Character;@24d46ca6
Charpter 2: Idioms
[Ljava.lang.Character;@4517d9a3
Charpter 3: Kotlin by example
[Ljava.lang.Character;@372f7a8d
Charpter 4: Coding conventions
[Ljava.lang.Character;@2f92e0f4
有没有简单的方法通过重复一个字符来初始化字符串?
val chapters = arrayOf("Basic syntax", "Idioms", "Kotlin by example", "Coding conventions")
for (numCharpter in chapters.indices) {
val title = "Chapter ${numCharpter + 1}: ${chapters[numCharpter]}"
val line = "=".repeat(title.length)
println("$title\n$line\n")
}
val chapters = arrayOf("Basic syntax", "Idioms", "Kotlin by example", "Coding conventions")
val titles = chapters.mapIndexed { index, chapter ->
"Chapter ${index + 1}: $chapter".let { it + "\n" + "=".repeat(it.length) + "\n" }
}
for (title in titles) {
println(title)
}
或更具可读性:
val chapters = arrayOf("Basic syntax", "Idioms", "Kotlin by example", "Coding conventions")
val titles = chapters
.mapIndexed { index, chapter ->
"Chapter ${index + 1}: $chapter"
.let { title ->
buildString {
append(title)
append("\n")
append("=".repeat(title.length))
append("\n")
}
}
}
for (title in titles) {
println(title)
}
@lukas.j 的解决方案的进一步简化变体可能如下所示:
val chapters = arrayOf("Basic syntax", "Idioms", "Kotlin by example", "Coding conventions")
val titles = chapters
.mapIndexed { index, chapter ->
buildString {
append("Chapter ${index + 1}: $chapter")
append('\n')
repeat(length - 1) { append('=') }
append("\n")
}
}
titles.forEach { println(it) }
只是另一个版本,向我们的好朋友致敬 print
,您可以在同一行上继续打印字符。 (并不是说这是最好的方式,但了解您的选择是件好事!)
chapters.forEachIndexed { i, title ->
val heading = "Chapter ${i + 1}: $title"
println(heading)
repeat(heading.length) { print('=') }
print("\n\n")
}
所以它的组织方式与格式一样 - 打印您的 header,打印您的下划线,然后打印您需要的任何行分隔符。我认为这很容易阅读和使用(并且可能会成为一个很好的独立功能)。
如果您不喜欢 =====
行的分隔符被卷入最后的分隔符格式行,您可以随时执行类似
的操作
repeat(heading.length) { print('=') }.also { print("\n") } // or println()
如果你愿意的话。或者 run
或 let
,没关系,also
只是读起来像“哦,最后是这个”
我正在尝试在标题下方打印一行。这个想法是该行与标题的长度相同。我尝试了几种方法,我认为这是最接近的,但它给我的结果不正确。
fun main() {
val chapters = arrayOf("Basic syntax", "Idioms", "Kotlin by example", "Coding conventions")
for (numCharpter in chapters.indices){
// Print the name of the chapter
val title = "Charpter ${numCharpter + 1}: ${chapters[numCharpter]}"
val arrayDash = Array(title.length) {'='}
val stringDash = arrayDash.toString()
println("$title\n$stringDash\n")
// the rest of the code
}
}
我想要的输出是:
Charpter 1: Basic syntax
========================
Charpter 2: Idioms
==================
Charpter 3: Kotlin by example
=============================
Charpter 4: Coding conventions
==============================
我得到的输出是:
Charpter 1: Basic syntax
[Ljava.lang.Character;@24d46ca6
Charpter 2: Idioms
[Ljava.lang.Character;@4517d9a3
Charpter 3: Kotlin by example
[Ljava.lang.Character;@372f7a8d
Charpter 4: Coding conventions
[Ljava.lang.Character;@2f92e0f4
有没有简单的方法通过重复一个字符来初始化字符串?
val chapters = arrayOf("Basic syntax", "Idioms", "Kotlin by example", "Coding conventions")
for (numCharpter in chapters.indices) {
val title = "Chapter ${numCharpter + 1}: ${chapters[numCharpter]}"
val line = "=".repeat(title.length)
println("$title\n$line\n")
}
val chapters = arrayOf("Basic syntax", "Idioms", "Kotlin by example", "Coding conventions")
val titles = chapters.mapIndexed { index, chapter ->
"Chapter ${index + 1}: $chapter".let { it + "\n" + "=".repeat(it.length) + "\n" }
}
for (title in titles) {
println(title)
}
或更具可读性:
val chapters = arrayOf("Basic syntax", "Idioms", "Kotlin by example", "Coding conventions")
val titles = chapters
.mapIndexed { index, chapter ->
"Chapter ${index + 1}: $chapter"
.let { title ->
buildString {
append(title)
append("\n")
append("=".repeat(title.length))
append("\n")
}
}
}
for (title in titles) {
println(title)
}
@lukas.j 的解决方案的进一步简化变体可能如下所示:
val chapters = arrayOf("Basic syntax", "Idioms", "Kotlin by example", "Coding conventions")
val titles = chapters
.mapIndexed { index, chapter ->
buildString {
append("Chapter ${index + 1}: $chapter")
append('\n')
repeat(length - 1) { append('=') }
append("\n")
}
}
titles.forEach { println(it) }
只是另一个版本,向我们的好朋友致敬 print
,您可以在同一行上继续打印字符。 (并不是说这是最好的方式,但了解您的选择是件好事!)
chapters.forEachIndexed { i, title ->
val heading = "Chapter ${i + 1}: $title"
println(heading)
repeat(heading.length) { print('=') }
print("\n\n")
}
所以它的组织方式与格式一样 - 打印您的 header,打印您的下划线,然后打印您需要的任何行分隔符。我认为这很容易阅读和使用(并且可能会成为一个很好的独立功能)。
如果您不喜欢 =====
行的分隔符被卷入最后的分隔符格式行,您可以随时执行类似
repeat(heading.length) { print('=') }.also { print("\n") } // or println()
如果你愿意的话。或者 run
或 let
,没关系,also
只是读起来像“哦,最后是这个”