首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kotlin:如何通过重复给定次数的字符来创建字符串

Kotlin:如何通过重复给定次数的字符来创建字符串
EN

Stack Overflow用户
提问于 2022-01-26 16:10:39
回答 4查看 314关注 0票数 0

我正试图在标题下打印一条线。意思是这一行和标题的长度是一样的。我尝试了几种方法,我认为这是最接近的,但它给我的结果是不正确的。

代码语言:javascript
复制
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
    }
}

我想要的输出是:

代码语言:javascript
复制
Charpter 1: Basic syntax
========================

Charpter 2: Idioms
==================

Charpter 3: Kotlin by example
=============================

Charpter 4: Coding conventions
==============================

我得到的输出是:

代码语言:javascript
复制
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

是否有一种简单的方法通过重复一个字符来初始化一个字符串?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2022-01-26 16:17:43

代码语言:javascript
复制
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")
}
票数 3
EN

Stack Overflow用户

发布于 2022-01-26 17:00:00

@lukas.j解决方案的进一步简化可能如下所示:

代码语言:javascript
复制
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) }      
票数 2
EN

Stack Overflow用户

发布于 2022-01-26 16:30:48

代码语言:javascript
复制
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)
}

或更易读:

代码语言:javascript
复制
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)
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70866577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档