https://kotlinlang.org logo
Title
l

Lawrence

06/06/2022, 7:43 PM
is it possible to ignore new lines in a multiline string? In Java, one can use
\
to do so. Wondering if Kotlin has something similar
String a = "This is a long string";
String b = """
     This is a \
     long string""";
a.equals(b) // true
j

Joffrey

06/06/2022, 7:45 PM
I don't think there is any such thing syntax wise in Kotlin. However, you can post-process the string using
.replace("\n", "")
. This won't help with indents, though, which seems to also be removed in your Java example (if this behaves as you said it does). For indents, you have other utilities in Kolin called
trimIndent
and
trimMargin
l

Lawrence

06/06/2022, 7:46 PM
gotcha i was thinking i have to do something like that
yea if you put the closing
"""
in the same final line it strips out the indents in java (i think)
👌 1
n

nkiesel

06/06/2022, 10:22 PM
One workaround is to use
val b = """
    This is a 
    """.trimIndent() + """
    long string
    """.trimIndent()