-1

For example, if you have escaped string like Hello wo\\\\rld.txt you want to unescape it and make it, Hello wo\\rld.txt

essentially making, \\ -> \, \\r -> \r, \\n -> \n, etc

I tried doing string replace like:

out = out.replace("\\", "\"); but that is syntax error

  • I never used it but... _there is crate for that!_ Meet [unescape](https://crates.io/crates/unescape). – rodrigo Nov 15 '22 at 22:43

1 Answers1

0

You still have to escape every \ in your string literals i.e. out = out.replace("\\\\", "\\");

cafce25
  • 15,907
  • 4
  • 25
  • 31
  • If I replace "\\\\" with "\\" then, hello wo\\\\rld.txt will become: hello wo\\rld and in next step when I unescape \\r -> \r it will become, \r. This is incorrect. We have to preserve \\r – Sameer Shinde Nov 15 '22 at 23:05
  • Or avoid any escapes in literals by using raw literals:`out = out.replace(r"\\", r"\");` – Peter Hall Nov 16 '22 at 01:12