Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape triple quote within kotlin raw string

Tags:

string

kotlin

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)}"""
like image 708
tuesday Avatar asked Oct 17 '25 15:10

tuesday


1 Answers

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}"
like image 84
Sam Avatar answered Oct 20 '25 08:10

Sam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!