trimMargin

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

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

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

Doesn't preserve original line endings.

Example

assertEquals("ABC\n123\n456", """ABC
                            |123
                            |456""".trimMargin())

Parameters

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

Return deindented String

See Also

kotlin.text.isWhitespace