[英]How to escape special characters in Groovy, excluding unicode
I would like to escape all the special characters in a string except for unicode characters (\\u), since I would still like to display non-English characters correctly.我想转义字符串中除 unicode 字符 (\\u) 之外的所有特殊字符,因为我仍然希望正确显示非英文字符。 I am currently using the StringEscapeUtils.escapeJava() method, but have not found a way to exclude specific characters (\\u for unicode, in this instance).我目前正在使用 StringEscapeUtils.escapeJava() 方法,但还没有找到排除特定字符的方法(在这种情况下,\\u 表示 unicode)。 The only alternative I can think of is to call replace or replaceAll for each special character except \\u, which doesn't seem ideal.我能想到的唯一替代方法是为除 \\u 之外的每个特殊字符调用 replace 或 replaceAll,这似乎并不理想。 Is there a better way to do this?有一个更好的方法吗?
Ex:前任:
"Los
niños"
should be converted to "Los\\nniños"
instead of "Los\\nni\ños"
应该转换为"Los\\nniños"
而不是"Los\\nni\ños"
Ok, so if we look into the code of StringEscapeUtils
, we can see the escapeJava
method here :好的,所以如果我们查看StringEscapeUtils
的代码,我们可以在这里看到escapeJava
方法:
public static final String escapeJava(final String input) {
return ESCAPE_JAVA.translate(input);
}
Which uses ESCAPE_JAVA
which is defined here其中使用ESCAPE_JAVA
定义here
public static final CharSequenceTranslator ESCAPE_JAVA =
new LookupTranslator(
new String[][] {
{"\"", "\\\""},
{"\\", "\\\\"},
}).with(
new LookupTranslator(EntityArrays.JAVA_CTRL_CHARS_ESCAPE())
).with(
JavaUnicodeEscaper.outsideOf(32, 0x7f)
);
So, I think it's this last bit which is giving you the undesired escaping...所以,我认为这是最后一点给你不想要的逃避......
So we can roll our own... Given your input string:所以我们可以推出自己的...给定您的输入字符串:
def input = '''Los
niños'''
We can import the classes, and create our own LookupTranslator
(ripping off the first bits from commons-lang):我们可以导入类,并创建我们自己的LookupTranslator
(从 commons-lang 中提取第一部分):
import org.apache.commons.lang3.text.translate.EntityArrays
import org.apache.commons.lang3.text.translate.LookupTranslator
def translator = new LookupTranslator([["\"", "\\\""], ["\\", "\\\\"]] as String[][]).with(
new LookupTranslator(EntityArrays.JAVA_CTRL_CHARS_ESCAPE())
)
And then, we can escape your input String, and check the result is as expected:然后,我们可以转义您的输入字符串,并检查结果是否符合预期:
assert translator.translate(input) == 'Los \\nniños'
If you need the escaping for the unicode chars below 32 (but obviously, not the higher code chars), you can change your translator to:如果您需要转义 32 以下的 unicode 字符(但显然,不是更高的代码字符),您可以将翻译器更改为:
import org.apache.commons.lang3.text.translate.EntityArrays
import org.apache.commons.lang3.text.translate.JavaUnicodeEscaper
import org.apache.commons.lang3.text.translate.LookupTranslator
def translator = new LookupTranslator([["\"", "\\\""], ["\\", "\\\\"]] as String[][]).with(
new LookupTranslator(EntityArrays.JAVA_CTRL_CHARS_ESCAPE())
).with(JavaUnicodeEscaper.below(32))
All these are from commons-lang3 which I assume is the version you are using...所有这些都来自 commons-lang3,我认为这是您正在使用的版本...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.