Unicode转义中的Scala原始字符串错误 [英] Scala raw strings error in unicode escape
问题描述
在Scala字符串中,需要逐字包含以下内容:\usepackage{x}
.因此,理想的是
In a Scala String need to include this literal verbatim: \usepackage{x}
. Thus, desired would be that for
val s = """ ... \usepackage{X} ... """
println(s)
... \usepackage{X} ...
到目前为止的尝试包括
scala> """\usepackage{X}"""
<console>:1: error: error in unicode escape
"""\usepackage{X}"""
^
scala> raw"""\usepackage{X}"""
<console>:1: error: error in unicode escape
raw"""\usepackage{X}"""
^
单双引号字符串也不成功.
Single double-quoted strings prove unsuccessful as well.
跟随 http://docs.scala-lang.org/overviews/core/string-interpolation.html ,一个有效的示例包括
Following http://docs.scala-lang.org/overviews/core/string-interpolation.html , a working example includes
scala> raw"a\nb"
res1: String = a\nb
其中不涉及unicode情况.
which does not cover unicode cases.
推荐答案
您似乎正面临问题 SI-4706 :Unicode文字语法阻碍了三引号的常见用例.
You appear to be facing issue SI-4706: Unicode literal syntax thwarts common use cases for triple-quotes.
在Scala中,不仅在字符或字符串文字内部处理Unicode转义序列.以下代码可能并不明显:
In Scala, unicode escape sequences are processed not only inside character or string literals. It may not be obvious that the following code would work:
scala> 5 \u002B 10
res0: Int = 15
不幸的是,如果您不想完全禁用Unicode转义(-Xno-uescape
),似乎没有解决该问题的好方法.问题中建议的解决方法之一是分隔反斜杠字符:
Unfortunately, there doesn't seem to be a good way around this if you don't want to disable unicode escapes completely (-Xno-uescape
). One of workarounds suggested in the issue is separating the backslash character:
scala> """\""" + """usepackage{X}"""
res1: String = \usepackage{X}
这篇关于Unicode转义中的Scala原始字符串错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!