Java 将问题替换为  (撇号/单引号)和 \(反斜杠)一起

发布于 2024-11-14 13:28:32 字数 747 浏览 2 评论 0原文

我好像遇到问题了我有一个查询字符串,其中的值可以包含单引号。这将破坏查询字符串。所以我试图进行替换,将 ' 更改为 \'

这是一个示例代码:

"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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

撩起发的微风 2024-11-21 13:28:32

首先,如果您尝试对查询字符串的撇号进行编码,则需要对它们进行 URL 编码,而不是使用前导反斜杠进行转义。为此,请使用 URLEncoder.encode(String, String) (顺便说一句:第二个参数应始终为 "UTF-8")。其次,如果要将撇号的所有实例替换为反斜杠撇号,则必须使用前导反斜杠转义字符串表达式中的反斜杠。像这样:

"This is' it".replace("'", "\\'");

编辑:

我现在看到您可能正在尝试动态构建 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:

"This is' it".replace("'", "\\'");

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.

梦在深巷 2024-11-21 13:28:32

使用 "This is' it".replace("'", "\\'")

Use "This is' it".replace("'", "\\'")

后eg是否自 2024-11-21 13:28:32

我使用了一个技巧来处理撇号特殊字符。将 ' 替换为 \' 时,您需要在撇号前放置四个反斜杠。

str.replaceAll("'","\\\\'");

I have used a trick to handle the apostrophe special character. When replacing ' for \' you need to place four backslashes before the apostrophe.

str.replaceAll("'","\\\\'");
冷情妓 2024-11-21 13:28:32

如果你想在 JavaScript 中使用它,那么你可以使用

str.replace("SP","\\SP");

But in Java

str.replaceAll("SP","\\SP");

会完美地工作。

SP:特殊字符

否则你可以使用Apache的EscapeUtil。它会解决你的问题。

If you want to use it in JavaScript then you can use

str.replace("SP","\\SP");

But in Java

str.replaceAll("SP","\\SP");

will work perfectly.

SP: special character

Otherwise you can use Apache's EscapeUtil. It will solve your problem.

哭了丶谁疼 2024-11-21 13:28:32

请记住,stringToEdit.replaceAll(String, String)返回结果字符串。它不会修改 stringToEdit,因为字符串在 Java 中是不可变的。要坚持任何更改,您应该使用

stringToEdit = stringToEdit.replaceAll("'", "\\'");

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

stringToEdit = stringToEdit.replaceAll("'", "\\'");
请远离我 2024-11-21 13:28:32

例子 :

String teste = " 'Bauru '";

teste = teste.replaceAll("  '  ","");
JOptionPane.showMessageDialog(null,teste);

Example :

String teste = " 'Bauru '";

teste = teste.replaceAll("  '  ","");
JOptionPane.showMessageDialog(null,teste);
一萌ing 2024-11-21 13:28:32

我曾经用来

str.replace("'", "");

替换字符串中的单引号。它对我来说工作得很好。

I have used

str.replace("'", "");

to replace the single quote in my string. Its working fine for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文