gpt4 book ai didi

kotlin - 字符串文字中的转义字符串插值

转载 作者:行者123 更新时间:2023-12-02 06:46:39 40 4
gpt4 key购买 nike

在普通字符串中,我可以使用反斜杠转义 ${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."""

最佳答案

来自 kotlinlang.org :

如果您需要在原始字符串中表示文字 $ 字符(它不会
支持反斜杠转义),您可以使用以下语法:

val price = """
${'$'}9.99
"""

所以,在你的情况下:
"""This ${'$'}{variable} will not be substituted."""

关于kotlin - 字符串文字中的转义字符串插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59576925/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com