Scala字符串格式:转义序列

时间:2017-11-14 01:40:57

标签: scala string-formatting

如何解决涉及转义序列的以下字符串格式问题?

[~] 17:34:04$ scala
Welcome to Scala 2.11.8 (OpenJDK 64-Bit Server VM, Java 1.8.0_151).
Type in expressions for evaluation. Or try :help.

scala> val fullFootnoteId = "asdf"
fullFootnoteId: String = asdf

scala> val footnoteId = 1
footnoteId: Int = 1

scala>  val footnoteTag = s"<sup id=\"ref_${fullFootnoteId}\"><a href=\"#foot_${fullFootnoteId}\">${footnoteId}</a></sup>"
<console>:12: error: value ref_$ is not a member of String
        val footnoteTag = s"<sup id=\"ref_${fullFootnoteId}\"><a href=\"#foot_${fullFootnoteId}\">${footnoteId}</a></sup>"
                                      ^
<console>:12: error: value \ is not a member of String
        val footnoteTag = s"<sup id=\"ref_${fullFootnoteId}\"><a href=\"#foot_${fullFootnoteId}\">${footnoteId}</a></sup>"
                                                           ^

1 个答案:

答案 0 :(得分:1)

按照scala string formatting with interpolator S not working中的提示,替换为三引号“”“修复了问题。

  val footnoteTag = s"""<sup id=\"ref_${fullFootnoteId}\"><a href=\"#foot_${fullFootnoteId}\">${footnoteId}</a></sup>"""