W3cubDocs

/Kotlin

trimMargin

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun String.trimMargin(marginPrefix: String = "|"): String

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.*

fun main(args: Array<String>) {
//sampleStart
val withoutMargin1 = """ABC
                |123
                |456""".trimMargin()
println(withoutMargin1) // ABC\n123\n456

val withoutMargin2 = """
    #XYZ
    #foo
    #bar
""".trimMargin("#")
println(withoutMargin2) // XYZ\nfoo\nbar
//sampleEnd
}

Parameters

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

See Also

trimIndent

kotlin.text.isWhitespace

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