I am really missing a standard feature of other la...
# language-proposals
f
I am really missing a standard feature of other languages in Kotlin, and that is the ability to escape the line feed in a multiline string. I can do this easily in e.g. Bash or Python but there really is no nice solution in Kotlin. Especially not because line feeds are platform dependent (
\n
vs
\r\n
). Basically what we should have:
Copy code
println("""\
This is the first line in this \
multi-line string.

This is the second line in this \
multi-line string.
""")
The above would result in:
Copy code
This is the first line in this multi-line string.

This is the second line in this multi-line string.
I guess the fact that the backslash has no special meaning in multi-line strings today would be a blocker for this, because it suddenly would need to be escaped and existing code would break. At the same time it would have been nice to use it as escaper so that we could simply write
\$
instead of
${'$'}
but too late. I fear an alternative approach is needed here. Maybe
f""
for a multi-line string with normal escape behavior (so
\"
needs to be used). 🤔 Anyways, just wanted to get this off my chest.
âž• 1
e
you could consider a
.trimMargin()
-like approach: write your strings with an explicit | denoting the intentional start of a line, and create a
String
extension to strip everything else away
f
I stumbled upon this mostly in random Gradle build scripts, where I want to provide error messages that explain the user how they can fix the issue. Copying that extension from project to project to project (especially if they're maybe even open source) is not going to cut it. Depending on a plugin just for this also sounds like overkill. It really should be supported by the language.
I mostly use this:
Copy code
"""""".trimIndent().replace("(?<!\n)\n(?!\n)".toRegex(), " ")
Which obviously can have false positives.e
l
No need for a regex, the following should work just fine:
replace("\\\n", "")
f
If it would be that simple then we could do
replace('\n', ' ')
but this will replace
\n\n
and those are the ones I want to preserve. The Regex checks that it's a single
\n
and replaces it with a space.
Oh, wait, I'm an idiot and you're right, haven't noticed the backslash.
The Regex is without the backslash. 😛