我遇到了问题。我的查询字符串中可能包含单引号,这会导致查询字符串出现错误。因此,我尝试使用替换将'
更改为\'
。
以下是示例代码:
"This is' it".replace("'", "\'");
这仍然是它的输出:"This is' it".
它认为我只是在进行引号的转义字符。
所以我尝试了这两段代码:
"This is' it".replace("'", "\\'"); // \\ for the backslash, and a ' char
"This is' it".replace("'", "\\\'"); // \\ for the backslash, and \' for the ' char
以上两种方法仍会产生相同的输出:
"This is' it"
我只能用以下代码来打印出一个斜杠:
"This is' it".replace("'", "\\\\'");
导致以下结果:
"This is\\' it"
有什么建议吗? 我只想用\'
替换'
。
看起来应该不难。