val myQuestion = """
I am creating a multiline string containing paragraphs of text. The text will wrap when put into a TextView.
But as you can see, when defining the text in the editor, if I want to avoid newlines mid-paragraph, I need to write really long lines that require a lot of horizontal scrolling.
Is there some way that I can have line breaks in the editor that don't appear in the actual value of the string?
"""
CodePudding user response:
Inspired by how you can add $
in a multiline string (which you otherwise cannot do) by doing ${"$"}
, I've thought of this way of adding a line break in the multiline string literal, but not in the string value itself.
val myQuestion = """
I am creating a multiline string containing paragraphs of text.${ ""
} The text will wrap when put into a TextView.
But as you can see, when defining the text in the editor,${ ""
} if I want to avoid newlines mid-paragraph, ${ ""
} I need to write really long lines that require a lot of horizontal scrolling.
Is there some way that I can have line breaks in the editor that don't appear in the actual value of the string?
""".trimIndent()
(The indentation and trimIndent
are just there to make it look nice. They are not required for this to work.)
Basically, I'm exploiting the fact that you can put arbitrary whitespace in ${ ... }
, so how about putting a line break there? There still gotta be an expression in ${ ... }
though, so you have to write ""
to append nothing to the string.
CodePudding user response:
Another approach is to treat a single-newline as an "editor only" newline, which gets removed. If you actually want a single-newline put a double-newline. If you want a double, put a triple, and so-on:
val INFO_TEXT = """
I am creating a multiline string containing paragraphs of text.
The text will wrap when put into a TextView.
But as you can see, when defining the text in the editor,
if I want to avoid newlines mid-paragraph, I need to write
really long lines that require a lot of horizontal scrolling.
Is there some way that I can have line breaks in the editor
that don't appear in the actual value of the string?
""".trimIndent().replace(Regex("(\n*)\n"), "$1")