如何更换||(两个管道)从带有|的字符串中(一)管 [英] How to replace || (two pipes) from a string with | (one) pipe
问题描述
我在此标签中获得了一些json格式的图像的响应:
I am getting response for some images in json format within this tag:
"xmlImageIds":"57948916 || 57948917 || 57948918 || 57948919 || 57948920 || 57948921 || 57948 922 || 57948923 || 57948924 || 57948925 || 57948926 || 5794892"
"xmlImageIds":"57948916||57948917||57948918||57948919||57948920||57948921||57948 922||57948923||57948924||57948925||57948926||5794892"
我要做的是使用字符串类的.split("||")分隔每个图像ID.然后,将带有此图像ID的网址附加并显示出来.
What i want to do is to separate each image id using .split("||") of the string class. Then append url with this image id and display it.
我已经尝试过 .replace("\" | \"|","\" |);
,但它对我不起作用.请帮忙.
I have tried .replace("\"|\"|","\"|");
but its not working for me. Please help.
Shabbir,我试图根据您在下面的评论来更新您的问题.如果我做对的话,请重新编辑.
Shabbir, I tried to update your question according to your comments below. Please edit it again, if I didn't get it right.
推荐答案
使用
.replace("||", "|");
|
不是特殊字符.
但是,如果您使用的是 split()
或 replaceAll
而不是 replace()
,请注意,您需要对管道符号进行转义为 \\ |
,因为这些方法都使用正则表达式作为参数.
However, if you are using split()
or replaceAll
instead of replace()
, beware that you need to escape the pipe symbol as \\|
, because these methods take a regex as parameter.
例如:
public static void main(String[] args) {
String in = "\"xmlImageIds\":\"57948916||57948917||57948918||57948919||57948920||57948921||57948922||57948923||57948924||57948925||57948926||5794892\"".replace("||", "|");
String[] q = in.split("\"");
String[] ids = q[3].split("\\|");
for (String id : ids) {
System.out.println("http://test/" + id);
}
}
这篇关于如何更换||(两个管道)从带有|的字符串中(一)管的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!