gpt4 book ai didi

string - 如何在scala中使用插值将双引号插入到字符串中

转载 作者:行者123 更新时间:2023-12-02 14:17:05 26 4
gpt4 key购买 nike

无法转义函数中的所有引号

(它的基本用法 -> 如果我找到一个字符串,则不执行任何操作,如果它不是字符串,则在开头和结尾添加“)

代码片段:

  def putTheDoubleQuotes(value: Any): Any = {
value match {
case s: String => s //do something ...
case _ => s"\"$value\"" //not working
}
}

唯一有效的是:

case _ => s"""\"$value\""""

有更好的语法吗?

它看起来很糟糕,IDE(IntelliJ)将它标记为红色(但让你运行它,这真的让我很生气!!!!!)

最佳答案

这是 Scala 中的一个错误:

escape does not work with string interpolation

但也许你可以使用:

scala> import org.apache.commons.lang.StringEscapeUtils.escapeJava
import org.apache.commons.lang.StringEscapeUtils.escapeJava

scala> escapeJava("this is a string\nover two lines")
res1: java.lang.String = this is a string\nover two lines

关于string - 如何在scala中使用插值将双引号插入到字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21086263/

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