如何更换|| (两个管道)来自带有|的字符串 (一)管道

Sha*_*sha 2 java string

我在这个标签中得到了json格式的一些图像的响应:

"xmlImageIds":"57948916 || 57948917 || 57948918 || 57948919 || 57948920 || 57948921 || 57948 922 || 57948923 || 57948924 || 57948925 || 57948926 || 5794892"

我想要做的是使用字符串类的.split("||")分隔每个图像id.然后使用此图像ID附加url并显示它.

我试过.replace("\"|\"|","\"|");但它不适合我.请帮忙.

编辑:Shabbir,我试图根据您的评论更新您的问题.如果我没弄错,请再次编辑.

nfe*_*ner 14

使用

.replace("||", "|");
Run Code Online (Sandbox Code Playgroud)

| 没有特别的char.

但是,如果您正在使用split()replaceAll代替replace(),请注意您需要将管道符号转义为\\|,因为这些方法将正则表达式作为参数.

例如:

public static void main(String[] args) {
    String in = "\"xmlImageIds\":\"57948916||57948917||57948918||57948919||57948920||57948921||57948?922||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);
    }
}
Run Code Online (Sandbox Code Playgroud)