What is the correct way of defining a Kotlin string that includes the characters for declaring a template substitution, but not have this evaluated as a template?
For example: "${something}"
just treated as an ordinary string.
I would like to use the Spring value annotation:
@Value("${some.property}) lateinit var foobar : String?
To insert characters that are illegal in a string, use an escape character. An escape character is a backslash \ followed by the character you want to insert.
Kotlin string interpolation String interpolation is variable substitution with its value inside a string. In Kotlin, we use the $ character to interpolate a variable and ${} to interpolate an expression.
To ignoring escape sequences in the string, we make the string as "raw string" by placing "r" before the string. "raw string" prints as it assigned to the string. Python | How to print double quotes with the string variable?
This works for me:
val s = "\${foo}" println("s = ${s}") // prints s = ${foo}
The documented way also works fine:
val s = "${'$'}{foo}" println("s = ${s}") // prints s = ${foo}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With