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

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

    Remember that stringToEdit.replaceAll(String, String) returns the result string. It doesn't modify stringToEdit because Strings are immutable in Java. To get any change to stick, you should use

    stringToEdit = stringToEdit.replaceAll("'", "\\'");
    

提交回复
热议问题