英文:
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:
-
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. -
Your question is missing information: for example, you are receiving this
String
from a service which is responding in Json, and over the transport thisString
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 = "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:
-
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. -
Your question is missing information: for example, you are receiving this
String
from a service which is responding in Json, and over the transport thisString
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).
答案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("\\\\\\\"", "\"");
I think that's right.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论