trimIndent

Common
JVM
JS
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