Escape ${something} in a Kotlin String

2019-04-18 23:55发布

问题:

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?

回答1:

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}


标签: kotlin