How can I interpret escape sequences in a multiline scala string?

escaping, multiline, scala, string-interpolation

Solution

Two options that I can think of:

You can use `StringContext.treatEscapes` directly:

StringContext.treatEscapes("""I want to be able to
        |have the convenient formatting of a multiline string,
        |while using inline escape sequences\r\r\b\\
        |
        |How can this be done?""".stripMargin)

If the variable substitution feature of the "simple interpolator" (`s`) isn't disruptive to your needs, then try combining string interpolation (which converts escaped characters) with `"""`-quotes (which doesn't escape ...):

println("""he\\lo\nworld""")
println(s"""he\\lo\nworld""")

outputs

he\\lo\nworld
he\lo
world

For details, see the relevant SIP and this earlier question.

Problem

In a nutshell: ``` """I want to be able to |have the convenient formatting of a multiline string, |while using inline escape sequences\r\r\b\\ | |How can this be done?""".stripMargin ```

Original source

Related problems