Goal:
one\n
two
Onboard JavaScript requires us to break the code formatting.
var lines = `one
two`
Groovy gives us another option: add a leading slash and newline, then call stripIndent()
.
def lines1 = """one
two"""
def lines2 = """\
one
two""".stripIndent()
Kotlin is similar, but the second option requires no slash because trimIdent()
considers the leading newline as indentation. 🤔
val lines1 = """one
two"""
val lines2 = """
one
two""".trimIndent()
Java requires a leading newline:
var lines = """
one
two""";
Goal:
one\n
two\n
JavaScript: Add the newline (code formatting interruption gets worse!).
var lines = `one
two
`
Groovy: Add the newline (in option 1, code formatting interruption gets worse!).
def lines1 = """one
two
"""
def lines2 = """\
one
two
""".stripIndent()
Kotlin: Add the newline (in option 1, code formatting interruption gets worse!).
val lines1 = """one
two
"""
val lines2 = """
one
two
""".trimIndent()
Java: Add the newline.
var lines = """
one
two
""";
Goal:
one
two
JavaScript: Add whitespace to line with opening delimiter (bonkers) and second line.
var lines = ` one
two
`
Groovy:
- Add whitespace to line with opening delimiter (bonkers) and second line, or
- Add Asciiart and switch from
stripIndent
tostripMargin
.
def lines1 = """ one
two
"""
def lines2 = """\
| one
| two
""".stripMargin()
Again, Kotlin is very similar to Groovy with the bonkers option 1 and the Asciiart plus method switch in the other case:
val lines1 = """ one
two
"""
val lines2 = """
| one
| two
""".trimMargin()
Java: Indent each line.
var lines = """
one
two
""";