在Java中,我想要将所有
例如: 文本:
*
字符替换为\*
。例如: 文本:
select * from blah
结果:select \\* from blah
public static void main(String[] args) {
String test = "select * from blah";
test = test.replaceAll("*", "\\*");
System.out.println(test);
}
这种方法不起作用,添加转义反斜杠也无效。
replaceAll
使用正则表达式,因此第一个参数是正则表达式*
。*
是正则表达式的 元字符,具有特殊含义。 你应该阅读正则表达式教程,例如:http://www.regular-expressions.info/tutorial.html。 - Richard Cook