如何将“\"”转义为绝对、具体、唯一、单一的值?Java

huangapple go评论112阅读模式
英文:

How to escape \" as absolute, concrete, unique, single value? Java

问题

I'm trying to replace \" backslash and quote to ", those characters are together in my text, so I do a replace but the compiler it say that my escaping syntax is incorrect:

String myText = "Animal:{"name":"turkey"}";

As you can observe here there is \" together so I'm want to replace by just one quote "
To look like this:

"Animal:{"name":"turkey"}"

So my replace its looks like:

myText.replaceAll("\"", """);

  • To escape a backslash we need \\
  • To escape a quote we need \"

With that logic I have this \\\" but its not working, its incorrect for the compiler...
I already tried:

myText.replaceAll("\*", "");
But this one is not want I want in my string.

Any advice?

英文:

I'm trying to replace \" backslash and quote to ", those characters are together in my text, so I do a replace but the compiler it say that my escaping syntax is incorrect:

String myText = "Animal:{\"name\":\"turkey\"}";

As you can observe here there is \" together so I'm want to replace by just one quote "
To look like this:

"Animal:{"name":"turkey"}"

So my replace its looks like:

myText.replaceAll("\\\"", "\"");
  • To escape a backslash we need \\
  • To escape a quote we need \"

With that logic I have this \\\" but its not working, its incorrect for the compiler...
I already tried:

myText.replaceAll("\\*", "");

But this one is not want I want in my string.

Any advice?

答案1

得分: 1

I think you are exposing the problem wrongly. If you write a String like this in the source code:

String myText = "Animal:{"name":"turkey"}";

... the String is already escaped. It means you may print:

System.out.println(myText);

... and you would get in output:

Animal:{"name":"turkey"}

... without need of doing anything else.

So I can only imagine two things:

  1. Your question is: Can I write String myText = "Animal:{"name":"turkey"}"; without backslashes in the source code? => The answer is no, or the compiler wouldn't know what's the delimiter of the text and what's just another character of the text.

  2. Your question is missing information: for example, you are receiving this String from a service which is responding in Json, and over the transport this String is keeping the backslashes on the quotes.

If that's the case, you should rather use the proper library to parse the message as JsonNode. Add a dependency to jackson-databind into your project and then use these methods:

ObjectMapper mapper = new ObjectMapper(); // <-- create ObjectMapper
JsonNode actualObj = mapper.readTree(jsonString); // <-- parse your Json string into a JsonNode
String niceJsonString = actualObj.toPrettyString(); // <-- formats the Json properly

If your question falls in my second guess, then I suggest you have a look at Jackson, it is a pretty powerful library to work with Json (a market standard for Json messaging in Java).

英文:

I think you are exposing the problem wrongly.
If you write a String like this in the source code:

String myText = &quot;Animal:{\&quot;name\&quot;:\&quot;turkey\&quot;}&quot;;

... the String is already escaped.
It means you may print :

System.out.println(myText);

... and you would get in output:

Animal:{&quot;name&quot;:&quot;turkey&quot;}

... without need of doing anything else.

So I can only imagine two things:

  1. Your question is: Can I write String myText = &quot;Animal:{&quot;name&quot;:&quot;turkey&quot;}&quot;; without backslashes in the source code? => The answer is no, or the compiler wouldn't know what's the delimiter of the text and what's just another character of the text.

  2. Your question is missing information: for example, you are receiving this String from a service which is responding in Json, and over the transport this String is keeping the backslashes on the quotes.

If that's the case, you should rather use the proper library to parse the message as JsonNode. Add a dependency to jackson-databind into your project and then use these methods:

ObjectMapper mapper = new ObjectMapper(); // &lt;-- create ObjectMapper
JsonNode actualObj = mapper.readTree(jsonString); // &lt;-- parse your Json string into a JsonNode
String niceJsonString = actualObj.toPrettyString(); // &lt;-- formats the Json properly

If your question falls in my second guess, then I suggest you have a look at Jackson, it is a pretty powerful library to work with Json (a market standard for Json messaging in Java).

答案2

得分: 0

问题在于replaceAll的第一个参数不仅仅是一个字符串,它是一个正则表达式字符串,并且正则表达式也使用反斜杠来转义下一个字符。所以你需要再多一层转义,以便传递给正则表达式的是你拥有的字符串,即转义反斜杠和引号,这样正则表达式将搜索反斜杠引号序列。

myText.replaceAll("\\\\\"", "\\\"");

我认为这是正确的。

英文:

The problem is that the first argument of replaceAll is not just a String, it is a regex string, and regex also uses backslash to escape the next character. So you need another entire level of escaping, so that what you are passing to regex is the string you have, i.e. escape backslash escape quote, so that the regex will search for the sequence backslash quote.

myText.replaceAll(&quot;\\\\\\\&quot;&quot;, &quot;\&quot;&quot;);

I think that's right.

huangapple
  • 本文由 发表于 2020年7月23日 03:27:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63041774.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定