Home > Mobile >  Escape triple quote within kotlin raw string
Escape triple quote within kotlin raw string

Time:01-24

I'm trying to create a raw string that contains three quotes in itself.

The resulting string x should contain something like """abc""". I've been able to create the string with the following code, but was wondering if there's a simpler solution for this.

val x = """${'"'.toString().repeat(3)}abc${'"'.toString().repeat(3)}"""

CodePudding user response:

There's no easy way to use a triple quote directly in a string literal.

One workaround I've sometimes used is to make an interim variable to hold the triple-quote string.

val quotes = "\"\"\""
val result = "${quotes}abc${quotes}"

CodePudding user response:

I think a simpler way would be to escape them manually, so like:

val x = "\"\"\"abc\"\"\""
  • Related