我需要转义字符串中的所有引号('),这样它就变成了\‘
我试过使用replaceAll,但它什么也做不了。由于某些原因,我不能让正则表达式工作。
我正在试着
String s = "You'll be totally awesome, I'm really terrible";
String shouldBecome = "You\'ll be totally awesome, I\'m really terrible";
s = s.replaceAll("'","\\'"); // Doesn't do anything
s = s.replaceAll("\'","\\'"); // Doesn't do anything
s = s.replaceAll("\\'","\\'"); // Doesn't do anything
我真的被困在这里了,希望有人能帮我。
谢谢,
Iwan
发布于 2013-12-13 07:09:35
您还可以尝试使用像StringEscapeUtils这样的工具来让您的工作更轻松:http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html
s = StringEscapeUtils.escapeJava(s);
发布于 2020-04-08 23:26:38
可以使用apache的commons-text库(而不是commons-lang):
示例代码:
org.apache.commons.text.StringEscapeUtils.escapeJava(escapedString);
依赖关系:
compile 'org.apache.commons:commons-text:1.8'
OR
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.8</version>
</dependency>
https://stackoverflow.com/questions/20556101
复制相似问题