Java replace issues with ' (apostrophe/single quote) and \ (backslash) together

前端 未结 7 1685
梦毁少年i
梦毁少年i 2021-01-30 18:03

I seem to be having issues. I have a query string that has values that can contain single quotes. This will break the query string. So I was trying to do a replace to change

7条回答
  •  無奈伤痛
    2021-01-30 18:12

    If you want to use it in JavaScript then you can use

    str.replace("SP","\\SP");
    

    But in Java

    str.replaceAll("SP","\\SP");
    

    will work perfectly.

    SP: special character

    Otherwise you can use Apache's EscapeUtil. It will solve your problem.

提交回复
热议问题