在正常的字符串中,我可以使用反斜杠转义${variable}
:
"You can use \${variable} syntax in Kotlin."
在字符串字面量中是否可以做相同的事情?反斜杠不再是转义字符:
// Undesired: Produces "This \something will be substituted.
"""This \${variable} will be substituted."""
到目前为止,我看到的唯一解决方案是字符串拼接,这样做非常丑陋,并嵌套插值,这开始变得有点荒谬:
// Desired: Produces "This ${variable} will not be substituted."
"""This ${"\${variable}"} will not be substituted."""
"This \something will be substituted."
还是"This ${variable} will not be substituted."
? - deHaar$
和{
之间添加一个空格,这样不会影响格式。 - Harshvardhan Joshi