开发者

Problem with \' in URLencoding

开发者 https://www.devze.com 2023-01-02 05:06 出处:网络
I am working on Java. Here is my code response = URLEncoder.encode(response, \"UTF-8\").replaceAll(\"\\\\+\", \"%20\");

I am working on Java. Here is my code

response = URLEncoder.encode(response, "UTF-8").replaceAll("\\+", "%20");

Problem comes when there is a ' (single quote) in the string response. It gets encoded to \'.

eg - 'Collective Dynamics of Complex Networks' comes as

\'Collective Dynamics of Complex Networks\'

I 开发者_JS百科want it to remain as it is. What should I do?


This may work:

String after = before.replace("\\'", "'");

This assigns to after, before with \' replaced with '.

API links

  • String replace(CharSequence target, CharSequence replacement)
    • Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".
0

精彩评论

暂无评论...
验证码 换一张
取 消