kotlin

Escape ${something} in a Kotlin String


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?

Solution

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