什么是Java 1.4.2等效的Pattern.quote() [英] What is the Java 1.4.2 equivalent of Pattern.quote()
本文介绍了什么是Java 1.4.2等效的Pattern.quote()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是Java 1.4.2等效的Pattern.quote?
What would be a Java 1.4.2 equivalent of Pattern.quote?
我在URI上使用Pattern.quote()但现在需要将其设为1.4 .2兼容。
I was using Pattern.quote() on a URI but now need to make it 1.4.2 compatible.
推荐答案
Pattern.quote
的源代码可用并且看起来很像像这样:
Well the source code of Pattern.quote
is available and looks like this:
public static String quote(String s) {
int slashEIndex = s.indexOf("\\E");
if (slashEIndex == -1)
return "\\Q" + s + "\\E";
StringBuilder sb = new StringBuilder(s.length() * 2);
sb.append("\\Q");
slashEIndex = 0;
int current = 0;
while ((slashEIndex = s.indexOf("\\E", current)) != -1) {
sb.append(s.substring(current, slashEIndex));
current = slashEIndex + 2;
sb.append("\\E\\\\E\\Q");
}
sb.append(s.substring(current, s.length()));
sb.append("\\E");
return sb.toString();
}
基本上它依赖于
\Q Nothing, but quotes all characters until \E
\E Nothing, but ends quoting started by \Q
并且特殊处理 \ E
的情况字符串。
and has a special treatement of the case in which \E
is present in the string.
这篇关于什么是Java 1.4.2等效的Pattern.quote()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文