如何将转义字符添加到Java字符串?

如果我有一个字符串变量:

String example = "Hello, I'm here"; 

我想在变量中的每个''前面添加一个转义字符(即实际上没有逃脱字符),我该怎么做?

我不是在这里声称优雅,但我认为它可以做你想做的事(如果我弄错了,请纠正我):

 public static void main(String[] args) { String example = "Hello, I'm\" here"; example = example.replaceAll("'", "\\\\'"); example = example.replaceAll("\"", "\\\\\""); System.out.println(example); } 

输出

 Hello, I\'m\" here