kotlin 如何将多行字符串划分为一个字符串列表,每个字符串包含特定数量的行?

rdlzhqv9  于 2023-05-29  发布在  Kotlin
关注(0)|答案(2)|浏览(170)

我有一个这样的字符串:

1
00:01:22,416 --> 00:01:25,146
Hey, Jack, those racks
don't go up there.

2
00:01:25,153 --> 00:01:27,915
- What do you mean?
- They go in the parking lot.

3
00:01:27,921 --> 00:01:30,315
The ones with blue plastic go up there.

4
00:01:30,324 --> 00:01:34,161
Ah, okay, hey, sorry, I didn't realize.

我想把字符串分成一个字符串列表,每个字符串包含特定数量的行,例如,如果数字是5,每个字符串应该包含5行,如果还有剩余,它将在另一个字符串中。如何做到这一点?

vsikbqxv

vsikbqxv1#

下面是Kotlin中的示例代码。因为标签是Kotlin,我想你需要Kotlin代码。你也可以在这里测试:https://pl.kotl.in/GnuwwQSxd

fun divideStringIntoChunks(input: String, chunkSize: Int): List<String> {
    val lines = input.lines().map { it.trim() }
    val chunks = lines.chunked(chunkSize)
    return chunks.map { it.joinToString("\n") }
}

fun main() {
    val input = """
        1
        00:01:22,416 --> 00:01:25,146
        Hey, Jack, those racks
        don't go up there.

        2
        00:01:25,153 --> 00:01:27,915
        - What do you mean?
        - They go in the parking lot.

        3
        00:01:27,921 --> 00:01:30,315
        The ones with blue plastic go up there.

        4
        00:01:30,324 --> 00:01:34,161
        Ah, okay, hey, sorry, I didn't realize.
    """.trimIndent()

    val chunkSize = 5
    val dividedStrings = divideStringIntoChunks(input, chunkSize)
    dividedStrings.forEachIndexed { index, str ->
        println("String ${index + 1}:")
        println(str)
        println()
    }
}
uklbhaso

uklbhaso2#

val countOfSubtitles = 400

val result = input
  .split("\n\n")
  .chunked(countOfSubtitles)
  .map { it.joinToString("\n\n") }

另一个选项-取决于应用程序中稍后对块发生的事情-将交替地将输入字符串转换为数据类示例的List。可能会使数据处理更容易。沿着这个:

data class Subtitle (
  val number: Int,
  val timecodeStart: String,
  val timecodeEnd: String,
  val subtitles: List<String>
)

val subtitles = input
  .trimIndent()
  .split("\n\n")
  .filter { it.isNotEmpty() }
  .map {
    val item = it.split("\n")
    Subtitle(
      number = item[0].toInt(),
      timecodeStart = item[1].substringBefore(" --> "),
      timecodeEnd = item[1].substringAfter(" --> "),
      subtitles = item.slice(2 until item.size)
    )
  }
  // .chunked(400)   // optional

subtitles.forEach(::println)

输出:

Subtitle(number=1, timecodeStart=00:01:22,416, timecodeEnd=00:01:25,146, subtitles=[Hey, Jack, those racks, don't go up there.])
Subtitle(number=2, timecodeStart=00:01:25,153, timecodeEnd=00:01:27,915, subtitles=[- What do you mean?, - They go in the parking lot.])
Subtitle(number=3, timecodeStart=00:01:27,921, timecodeEnd=00:01:30,315, subtitles=[The ones with blue plastic go up there.])
Subtitle(number=4, timecodeStart=00:01:30,324, timecodeEnd=00:01:34,161, subtitles=[Ah, okay, hey, sorry, I didn't realize.])
Subtitle(number=5, timecodeStart=00:12:34,567, timecodeEnd=00:12:56,789, subtitles=[Ah, okay, hey, sorry, I did suddenly realize.])

相关问题