java 如何用 & 替换 \u0026?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17372098/
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 to replace \u0026 with &?
提问by Yan Gao
In Java, I need to change this:
在 Java 中,我需要改变这一点:
myid=460\u0026url=http%3A%2F%2Fr20-xxxx
myid=460\u0026url=http%3A%2F%2Fr20-xxxx
...to this:
...到这个:
myid=460&url=http%3A%2F%2Fr20-xxxx
myid=460&url=http%3A%2F%2Fr20-xxxx
Here's what I've tried:
这是我尝试过的:
String map = "myid=460\u0026url=http%3A%2F%2Fr20-xxxx";
p = Pattern.compile("\u0026");
m = p.matcher(map);
if (m.find()) {
String ret = m.replaceAll("&");
}
...but it cannot find the \u0026
.
...但它找不到\u0026
.
回答by rgettman
If you must use a regex, then you must escape the backslash that is in the Java string. Then you must escape both backslashes for regex interpretation. Try
如果必须使用正则表达式,则必须对 Java 字符串中的反斜杠进行转义。然后您必须转义两个反斜杠以进行正则表达式解释。尝试
p = Pattern.compile("\\u0026");
But a simple replace
should suffice (it doesn't use regex), with only one iteration of escape the backslash, for Java:
但是一个简单的replace
应该就足够了(它不使用正则表达式),对于 Java,只有一次转义反斜杠的迭代:
ret = map.replace("\u0026", "&");
回答by wegrata
Doesn't something as simple as
事情不是那么简单
"myid=460\u0026url=http%3A%2F%2Fr20-xxxx".replace("\u0026", "&");
work?
工作?