Jig*_*uff 10 java string escaping
如果我有一个字符串变量:
String example = "Hello, I'm here";
Run Code Online (Sandbox Code Playgroud)
我想在每个变量前面'和" 变量内添加一个转义字符(即实际上不会转义字符),我该怎么做?
iro*_*ion 19
我不是在这里声称优雅,但我认为它可以做你想做的事(如果我弄错了,请纠正我):
public static void main(String[] args)
{
String example = "Hello, I'm\" here";
example = example.replaceAll("'", "\\\\'");
example = example.replaceAll("\"", "\\\\\"");
System.out.println(example);
}
Run Code Online (Sandbox Code Playgroud)
输出
Hello, I\'m\" here
Run Code Online (Sandbox Code Playgroud)