如何在scala字符串中转义特殊符号?

fre*_*low 5 string scala escaping string-literals

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.

For example, the string \" (length 2) should be converted to the string "\\\"" (length 6).

Shr*_*rey 10

用3个引号包装字符串以按原样表示它.

例如 val str = """ \" """ // str : java.lang.String = \"


Mal*_*off 5

看看Apache Common的StringEscapeUtils类(这里的文档).escapeJava应该完成工作.

看看这个例子,看看它的实际效果(用Java).