fun String.trimMargin(marginPrefix: String = "|"): String (source)

Trims leading whitespace characters followed by marginPrefix from every line of a source string and removes the first and the last lines if they are blank (notice difference blank vs empty).

Doesn't affect a line if it doesn't contain marginPrefix except the first and the last blank lines.

Doesn't preserve the original line endings.

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

fun main(args: Array<String>) {
val withoutMargin1 = """ABC
println(withoutMargin1) // ABC\n123\n456

val withoutMargin2 = """
println(withoutMargin2) // XYZ\nfoo\nbar


marginPrefix - non-blank string, which is used as a margin delimiter. Default is | (pipe character).

See Also