如何解读多行scala字符串中的转义序列? [英] How can I interpret escape sequences in a multiline scala string?
本文介绍了如何解读多行scala字符串中的转义序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
简而言之:
我想要能够
|有方便的格式化一个多行字符串
|,而使用内联转义序列\\\\\\\\ $
|
? $ b
解决方案
我可以想到的两个选项:
-
您可以直接使用
StringContext.treatEscapes
StringContext.treatEscapes(我想要能够
|具有方便的格式化多行字符串
|,而使用内联转义序列\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ $ pre> -
如果简单插值器(
s
)的变量替换功能不是破坏性的根据您的需要,然后尝试结合字符串插入(它转义转义的字符)与-quotes(它不会逃脱...) :
println(he\\\\\
n)
println 他,)
输出
he\\lo\\\
world
he\lo
world
In a nutshell:
"""I want to be able to
|have the convenient formatting of a multiline string,
|while using inline escape sequences\r\r\b\\
|
|How can this be done?""".stripMargin
解决方案
Two options that I can think of:
You can use
StringContext.treatEscapes
directly:StringContext.treatEscapes("""I want to be able to |have the convenient formatting of a multiline string, |while using inline escape sequences\r\r\b\\ | |How can this be done?""".stripMargin)
If the variable substitution feature of the "simple interpolator" (
s
) isn't disruptive to your needs, then try combining string interpolation (which converts escaped characters) with"""
-quotes (which doesn't escape ...):println("""he\\lo\nworld""") println(s"""he\\lo\nworld""")
outputs
he\\lo\nworld he\lo world
For details, see the relevant SIP and this earlier question.
这篇关于如何解读多行scala字符串中的转义序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文