scala 用。。。来代替 \”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12115187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace " with \"
提问by blue-sky
How do I replace " with \".
如何将“替换为\”。
Here is what im trying :
这是我正在尝试的内容:
def main(args:Array[String]) = {
val line:String = "replace \" quote";
println(line);
val updatedLine = line.replaceAll("\"" , "\\"");
println(updatedLine);
}
output :
输出 :
replace " quote
replace " quote
The output should be :
输出应该是:
replace " quote
replace \" quote
采纳答案by pedrofurla
Two more \\does the job:
还有两个\\工作:
scala> line.replaceAll("\"" , "\\\"");
res5: java.lang.String = replace \" quote
The problem here is that there are two 'layers' escaping the strings. The first layer is the compiler, which we can easily see in the REPL:
这里的问题是有两个“层”在逃避字符串。第一层是编译器,我们在 REPL 中很容易看到:
scala> "\""
res0: java.lang.String = "
scala> "\"
res1: java.lang.String = \
scala> "\\""
res2: java.lang.String = \"
scala> val line:String = "replace \" quote";
line: String = replace " quote
The second layer is the regular expression interpreter. This one is harder to see, but can be seen by applyin your example:
第二层是正则表达式解释器。这个更难看到,但可以通过 applyin 你的例子看到:
scala> line.replaceAll("\"" , "\\"");
res5: java.lang.String = replace " quote
What the reg. exp. interpreter really receives is \", which is interpreted as only ". So, we need the reg. exp. to receive \\". To make the compiler give us \ we need to write \\.
什么注册。经验。解释器真正接收的是\",它被解释为只有"。所以,我们需要注册。经验。接收\\"。为了让编译器给我们\,我们需要写\\。
Let's see the unescaping:
让我们看看未转义:
- The right case: \\\" the compiler sees \", the regular expression sees \".
- The wrong case: \\" the compiler sees \", the regular expression sees ".
- 正确的情况:\\\"编译器看到\",正则表达式看到\"。
- 错误的情况:\\"编译器看到\",正则表达式看到"。
It can be a bit confusing despite being very straight forward.
尽管非常直截了当,但它可能有点令人困惑。
As pointed by @sschaef, another alternative it to use """ triple-quoting, strings in this form aren't unescaped by the compiler:
正如@sschaef 所指出的,另一种使用 """ 三重引用的替代方法,这种形式的字符串不会被编译器转义:
scala> line.replaceAll("\"" , """\"""");
res6: java.lang.String = replace \" quote
回答by Eunwoo Song
Use "replaceAllLiterally" method of StringOps class. This replaces all literal occurrences of the argument:
使用 StringOps 类的“replaceAllLiterally”方法。这将替换参数的所有文字出现:
scala> val line:String = "replace \" quote"
line: String = replace " quote
scala> line.replaceAllLiterally("\"", "\\"")
res8: String = replace \" quote
回答by Ram Rajamony
@pedrofurla nicely explains why you saw the behavior you did. Another solution to your problem would be to use a raw string with scala's triple-quote character. Anything between a pair of triple-quotes is treated as a raw string with no interpretation by the Scala compiler. Thus:
@pedrofurla 很好地解释了为什么您会看到自己的行为。您的问题的另一个解决方案是使用带有 Scala 的三引号字符的原始字符串。一对三引号之间的任何内容都被视为原始字符串,Scala 编译器不会对其进行解释。因此:
scala> line.replaceAll("\"", """\"""")
res1: String = replace \" quote
Used in conjunction with stripMargin, triple-quotes are a powerful way to embed raw strings into your code. For example:
与 结合使用stripMargin,三引号是一种将原始字符串嵌入到代码中的强大方法。例如:
val foo = """
|hocus
|pocus""".stripMargin
yields the string: "\nhocus\npocus"
产生字符串: "\nhocus\npocus"

