W3cubDocs

/Kotlin

trimIndent

fun String.trimIndent(): String

Detects a common minimal indent of all the input lines, removes it from every line and also removes the first and the last lines if they are blank (notice difference blank vs empty).

Note that blank lines do not affect the detected indent level.

In case if there are non-blank lines with no leading whitespace characters (no indent at all) then the common indent is 0, and therefore this function doesn't change the indentation.

Doesn't preserve the original line endings.

import kotlin.test.*
import java.util.*

fun main(args: Array<String>) {
//sampleStart
val withoutIndent =
        """
            ABC
            123
            456
        """.trimIndent()
println(withoutIndent) // ABC\n123\n456
//sampleEnd
}

See Also

trimMargin

kotlin.text.isBlank

© 2010–2018 JetBrains s.r.o.
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/trim-indent.html