Search code examples
stringkotlinescaping

How to embed a triple double quote in a raw string


Suppose we have a raw string as follows:

val myRawString = """line 1
line2
line3"""

Suppose the value we want to represent as a raw string has a line which itself contains a triple double quote. For instance, line4 should be this line has """ (triple double quote) How can we accomplish that?


Solution

  • You cannot escape anything in a raw string, but you can use string templates, and you can escape inside those, so you could do this:

    val myRawString = """line 1
    line2
    line3
    this line has ${"\"\"\""} (triple double quote)
    """