Raw / triple-quoted strings don't preserve the line separator of the system

It seems like triple-quoted strings don’t preserve the line separator of the system.

For example, on Windows this:

"""
   -first line
   -second line
""".trimIndent()

compiles to

"-first line\n-secondline"

instead of

"-first line\r\n-second line"

Is this by design? Is there any official documentation about line separators in triple-quoted strings?

Yes. It is so by design. There is a key requirement that behavior of the code should not depend on the platform it was compiled on. If you want platform line separators, I’d suggest:

"""
   -first line
   -second line
""".trimIndent().replace("\n", System.lineSeparator())
3 Likes

That makes sense thank you.

I recommend adding a short comment about line separators in this section of the docs:

We initially assumed that there was a problem with our environment setup so it took a while to narrow in on the root cause.

I’ve created KT-56021 Docs: Mention that multiline strings always use NL separators regardless of the platform.

3 Likes