W3cubDocs

/Kotlin

trimIndent

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
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.*

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–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/trim-indent.html