trimIndent

Common
JVM
JS
Native
1.0
fun String.trimIndent(): String
(source)

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.

The lines in the original string can be separated with \r\n (CRLF), \n (LF), or \r (CR) characters, however, the lines in the resulting string will be separated solely with \n (LF) character.

import java.util.Locale
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