'Kotlin: How to create a string by repeating a character a given number of times
I am trying to print a line below a title. The idea is that the line is the same length as the title. I have tried several ways and I think this is the closest, but the result it gives me is not correct.
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
}
}
the output i want is:
Charpter 1: Basic syntax
========================
Charpter 2: Idioms
==================
Charpter 3: Kotlin by example
=============================
Charpter 4: Coding conventions
==============================
the output i get is:
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
Is there a simple way to initialize a String by repeating a character?
Solution 1:[1]
A further simplified variation of the solution of @lukas.j might look like this:
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) }
Solution 2:[2]
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)
}
Or more readable:
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)
}
Solution 3:[3]
Just another version with a shoutout to our good buddy print, you can just keep printing characters on the same line. (Not saying this is the best way here, but it's good to know your options!)
chapters.forEachIndexed { i, title ->
val heading = "Chapter ${i + 1}: $title"
println(heading)
repeat(heading.length) { print('=') }
print("\n\n")
}
So it's organised like how it's formatted - print your header, print your underline, then print whatever line separators you need. I think that's pretty easy to read and work with (and would probably make for a good separate function).
If you don't like that the break for the ===== line is rolled into the final separator formatting line, you could always do something like
repeat(heading.length) { print('=') }.also { print("\n") } // or println()
if you wanted. Or run or let, it doesn't matter, also just reads like "oh and then this at the end"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Endzeit |
| Solution 2 | lukas.j |
| Solution 3 | cactustictacs |
