How to replace || (two pipes) from a string with | (one) pipe

后端 未结 5 1558
醉话见心
醉话见心 2021-01-15 00:24

I am getting response for some images in json format within this tag:

\"xmlImageIds\":\"57948916||57948917||57948918||57948919||57948920||57948921||57

5条回答
  •  孤城傲影
    2021-01-15 00:47

    Use

    .replace("||", "|");
    

    | is no special char.

    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.

    For example:

    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);
        }
    }
    

提交回复
热议问题