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

后端 未结 5 1565
醉话见心
醉话见心 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:40

    I think I know what your problem is. You need to assign the result of replace(), not just call it.

    String s = "foo||bar||baz";
    s = s.replace("||", "|");
    System.out.println(s);
    

    I tested it, and just calling s.replace("||", "|"); doesn't seem to modify the string; you have to assign that result back to s.

    Edit: The Java 6 spec says "Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar." (the emphasis is mine).

提交回复
热议问题