Java 将问题替换为 (撇号/单引号)和 \(反斜杠)一起
我好像遇到问题了我有一个查询字符串,其中的值可以包含单引号。这将破坏查询字符串。所以我试图进行替换,将 '
更改为 \'
。
这是一个示例代码:
"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"
有什么建议吗?我只想将 '
替换为 \'
。
看起来应该不那么困难。
I seem to be having issues. I have a query string that has values that can contain single quotes. This will break the query string. So I was trying to do a replace to change '
to \'
.
Here is a sample code:
"This is' it".replace("'", "\'");
The output for this is still:
"This is' it".
It thinks I am just doing an escape character for the quote.
So I tried these two pieces of code:
"This is' it".replace("'", "\\'"); // \\ for the backslash, and a ' char
"This is' it".replace("'", "\\\'"); // \\ for the backslash, and \' for the ' char
Both of the above STILL results in the same output:
"This is' it"
I can only seem to get this to actually spit out a slash with:
"This is' it".replace("'", "\\\\'");
Which results in:
"This is\\' it"
Any suggestions? I just want to replace a '
with \'
.
It doesn't seem like it should be that difficult.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
首先,如果您尝试对查询字符串的撇号进行编码,则需要对它们进行 URL 编码,而不是使用前导反斜杠进行转义。为此,请使用
URLEncoder.encode(String, String)
(顺便说一句:第二个参数应始终为"UTF-8"
)。其次,如果要将撇号的所有实例替换为反斜杠撇号,则必须使用前导反斜杠转义字符串表达式中的反斜杠。像这样:编辑:
我现在看到您可能正在尝试动态构建 SQL 语句。 不要这样做。您的代码很容易受到 SQL 注入攻击。请改用
PreparedStatement
。First of all, if you are trying to encode apostophes for querystrings, they need to be URLEncoded, not escaped with a leading backslash. For that use
URLEncoder.encode(String, String)
(BTW: the second argument should always be"UTF-8"
). Secondly, if you want to replace all instances of apostophe with backslash apostrophe, you must escape the backslash in your string expression with a leading backslash. Like this:Edit:
I see now that you are probably trying to dynamically build a SQL statement. Do not do it this way. Your code will be susceptible to SQL injection attacks. Instead use a
PreparedStatement
.使用
"This is' it".replace("'", "\\'")
Use
"This is' it".replace("'", "\\'")
我使用了一个技巧来处理撇号特殊字符。将 ' 替换为 \' 时,您需要在撇号前放置四个反斜杠。
I have used a trick to handle the apostrophe special character. When replacing ' for \' you need to place four backslashes before the apostrophe.
如果你想在 JavaScript 中使用它,那么你可以使用
But in Java
会完美地工作。
SP:特殊字符
否则你可以使用Apache的EscapeUtil。它会解决你的问题。
If you want to use it in JavaScript then you can use
But in Java
will work perfectly.
SP: special character
Otherwise you can use Apache's EscapeUtil. It will solve your problem.
请记住,
stringToEdit.replaceAll(String, String)
返回结果字符串。它不会修改 stringToEdit,因为字符串在 Java 中是不可变的。要坚持任何更改,您应该使用Remember that
stringToEdit.replaceAll(String, String)
returns the result string. It doesn't modify stringToEdit because Strings are immutable in Java. To get any change to stick, you should use例子 :
Example :
我曾经用来
替换字符串中的单引号。它对我来说工作得很好。
I have used
to replace the single quote in my string. Its working fine for me.