Java: Replace all ' in a string with \'

前端 未结 6 483
北恋
北恋 2020-12-29 04:30

I need to escape all quotes (\') in a string, so it becomes \\\'

I\'ve tried using replaceAll, but it doesn\'t do anything. For some reason I can\'t get the regex to

6条回答
  •  一整个雨季
    2020-12-29 05:23

    You can use apache's commons-text library (instead of commons-lang):

    Example code:

    org.apache.commons.text.StringEscapeUtils.escapeJava(escapedString);
    

    Dependency:

    compile 'org.apache.commons:commons-text:1.8'
    
    OR
    
    
       org.apache.commons
       commons-text
       1.8
    
    

提交回复
热议问题