简体   繁体   English

Java 1.4 String.replaceAll问题带有单引号

[英]Java 1.4 String.replaceAll issue with single quote

I've got a string value coming from a database that has to be put into JSON, but the value can contain single and double quotes. 我有一个来自必须放入JSON的数据库的字符串值,但是该值可以包含单引号和双引号。 Sounds easy enough, right? 听起来很容易,对吧? Well, apparently not in Java 1.4.2. 好吧,显然不是在Java 1.4.2中。 Don't get me started on why it has to be 1.4.2, I inherited this from another developer and we can't update this project yet due to factors beyond my control. 不要让我开始介绍为什么它必须是1.4.2,我是从另一个开发人员那里继承而来的,由于我无法控制的因素,我们还不能更新该项目。 :) :)

So, I have tried all of these (and lots of others just to see the result): 因此,我已经尝试了所有这些方法(还有很多其他方法只是为了查看结果):

"sample user's string".replaceAll("'", "\'")      // returns "sample user's string"
"sample user's string".replaceAll("'", "\\'")     // returns "sample user's string"
"sample user's string".replaceAll("'", "\\\'")    // returns "sample user's string"
"sample user's string".replaceAll("'", "\\\\'")   // returns "sample user\\'s string"
"sample user's string".replaceAll("'", "%%")      // returns "sample user%%s string"

All I want is sample user\\'s string , what am I doing wrong? 我想要的只是sample user\\'s string ,我在做什么错? Too bad 1.4.2 doesn't have the String.replace(String,String) function. 太糟糕了1.4.2没有String.replace(String,String)函数。

Edit : We are using the json-simple JSON library and I was looking at the output of the above commands in the resultant JSON string. 编辑 :我们正在使用json-simple JSON库,我正在结果JSON字符串中查看上述命令的输出。 I added additional debug info to see the value before the JSON gets output and it looks like the above commands really are working, but the json-simple library is stripping the escape chars out. 我添加了其他调试信息以在JSON输出之前查看该值,并且看起来上述命令确实有效,但是json-simple库正在剥离转义字符。

So, what I'm seeing is: 所以,我看到的是:

"sample user's string".replaceAll("'", "\\\\'")    // sample user\'s string
myJSONObject.put("value", "sample user's string".replaceAll("'", "\\\\'")) // sample user\\'s string"
myJSONObject.put("value", "sample user's string") // sample user's string

So it looks like the library is not doing its job quite as expected. 因此,图书馆似乎没有按预期完成其工作。 Does anyone that has used json-simple know of a workaround for this? 使用json-simple人是否知道解决方法?

I suspect you're looking at the string in a context which is automatically escaping backslashes for you. 我怀疑您是在自动为您转义反斜杠的上下文中查看字符串。 Your penultimate sample should be fine. 您倒数第二个样品应该没问题。 For example: 例如:

public class Test {
    public static void main(String[] args) {
        System.out.println("sample user's string".replaceAll("'", "\\\\'"));
    }
}

Prints: 印刷品:

sample user\'s string

You didn't say where you were looking at the value, but if it's in the debugger, that may well be doubling the backslash for you, to present the value as if it were in a Java literal. 您没有说要在哪里查看该值,但是如果它在调试器中,则很可能会使您的反斜杠加倍,以便像使用Java文字一样显示该值。

Ideally though, you shouldn't be doing this sort of thing manually - it's far too error-prone. 不过,理想情况下,您不应该手动执行此类操作-太容易出错了。 You should use a JSON library which handles all of this for you, so you just need to specify the value. 您应该使用一个为您处理所有这些的JSON库,因此您需要指定值即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM