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

前端 未结 7 1683
梦毁少年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:22

    I have used a trick to handle the apostrophe special character. When replacing ' for \' you need to place four backslashes before the apostrophe.

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

提交回复
热议问题