Pattern.quote 的 Java 1.4.2 等价物是什么?
我在 URI 上使用 Pattern.quote() 但现在需要使其与 1.4.2 兼容。
最佳答案
好吧,Pattern.quote
的源代码是可用的,如下所示:
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
的情况进行了特殊处理。
关于java - 什么是 Pattern.quote() 的 Java 1.4.2 等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3048489/