W3cubDocs

/Kotlin

trimMargin

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

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

© 2010–2017 JetBrains s.r.o.
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/trim-margin.html