0

Is it possible to make \"type4\" to "type4" in Java? Unable to find a way to escape this escape character.

earl
  • 738
  • 1
  • 17
  • 38
  • Does this answer your question? [escaping backslash in java string literal](https://stackoverflow.com/questions/23363241/escaping-backslash-in-java-string-literal) – OH GOD SPIDERS Jan 15 '20 at 09:39
  • 2
    use string.replace to remove the `\\`? Could you add some context, or even better, a reproductible example? For now it's hard to see exactly what you are trying to do – jhamon Jan 15 '20 at 09:39
  • Is `\"type4\"` the literal output you see (e.g. in a text file), or is that what you see in the console? – Tim Biegeleisen Jan 15 '20 at 09:39
  • @jhamon While inserting a document in mongo, \ was used a escape character. The API response picks the '\' as it is. I want to get rid of this '\' in API response. – earl Jan 16 '20 at 05:24
  • @TimBiegeleisen I see \"type\" in API response as well as in a text file – earl Jan 16 '20 at 05:26
  • @earl I attempted an answer below, though it seems somewhat dubious to me that this would be appearing in the API response, unless maybe it is JSON, in which case you should tell us this. – Tim Biegeleisen Jan 16 '20 at 05:31
  • @TimBiegeleisen It is a JSON indeed – earl Jan 16 '20 at 05:38
  • Then I doubt the text file has literal backslahses in it. You should investigate whatever tool wrote the file, because it made a mistake. – Tim Biegeleisen Jan 16 '20 at 05:39

1 Answers1

0
String replaced = "\\\"type4\\\"".replace("\\", "");
Egor
  • 1,334
  • 8
  • 22
  • 3
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – leopal Jan 15 '20 at 11:53