tuesday
tuesday

Reputation: 730

Escape triple quote within kotlin raw string

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)}"""

Upvotes: 4

Views: 730

Answers (3)

Arthur Khazbulatov
Arthur Khazbulatov

Reputation: 920

"""Here's a simpler, less verbose workaround: ${"\"\"\""}."""

Upvotes: 0

Sam
Sam

Reputation: 9944

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}"

Upvotes: 2

Ivo
Ivo

Reputation: 23178

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

val x = "\"\"\"abc\"\"\""

Upvotes: 0

Related Questions