string 如何在 Scala 字符串中转义特殊符号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17341079/
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
How can I escape special symbols in scala string?
提问by fredoverflow
Is there a general Scala utility method that converts a string to a string literal? The simple lambda function "\"" + _ + "\""
only works for strings without any special characters.
是否有将字符串转换为字符串文字的通用 Scala 实用程序方法?简单的 lambda 函数"\"" + _ + "\""
仅适用于没有任何特殊字符的字符串。
For example, the string \"
(length 2) should be converted to the string "\\\""
(length 6).
例如,\"
应该将字符串(长度为 2)转换为字符串"\\\""
(长度为 6)。
采纳答案by Malte Schwerhoff
Have a look at Apache Common's StringEscapeUtils
class (docs here). escapeJava
should get the job done.
看看 Apache Common 的StringEscapeUtils
类(这里的文档)。escapeJava
应该完成工作。
Have a look at this exampleto see it in action (in Java).
看看这个例子,看看它的实际效果(在 Java 中)。
回答by Shrey
Wrap the String with 3 quotes to represent it as is..
用 3 个引号包裹字符串以按原样表示它..
e.g.
val str = """ \" """ // str : java.lang.String = \"
例如
val str = """ \" """ // str : java.lang.String = \"