1

I have something like this:

"jo\\xe3o madeira"

And I want this:

"jo\xe3o madeira"

How can I replace the \\ for a single \ I have tried stuff like this:

gsub("\\\\", "\\", "jo\\xe3o madeira")

And the output was:

"joxe3o madeira"

So it completely removed the "\"

Thanks in advance

Miguel Santos
  • 1,806
  • 3
  • 17
  • 30
  • 3
    It already is 1 backslash. Try `nchar("jo\\xe3o madeira")` and `nchar("joxe3o madeira")` – Sotos Jan 04 '18 at 15:04
  • 1
    In case it's not clear from the answer/duplicate, when a string is printed by R it will "escape" special characters with a backslash. Special characters include backslashes. So the `\\` that you see printed is just a single `\` in the string, with an extra `\` printed to escape it. You can see whats "really" there by using `cat`, and indeed `cat("jo\\xe3o madeira")` will display the string with only the one backslash that is really there. The string only has one backslash, `cat` and `print` just offer different ways of looking at it. – Gregor Thomas Jan 04 '18 at 16:01

1 Answers1

2

You can do it using:-

str <- "jo\\xe3o madeira"
    cat(str)

This will give you:-

jo\xe3o madeira
user5249203
  • 4,436
  • 1
  • 19
  • 45
sm925
  • 2,648
  • 1
  • 16
  • 28
  • 1
    That doesn't remove any backslashes. Just `cat` the original string - there's only one backslash to start with. (What your `gsub` does is remove all quotes `"`. Since there aren't any, it has no effect). – Gregor Thomas Jan 04 '18 at 15:07
  • It's getting the output which was desired. – sm925 Jan 04 '18 at 15:41
  • 1
    The `gsub` is pointless. `cat(str)` gives the output that was desired. The lesson is "use `cat` not `print` to see the string printed without escaping backslashes". Having the `gsub` to remove a quote and saying that it *"It'll just remove one backslash"* will just confuse people because it is incorrect. – Gregor Thomas Jan 04 '18 at 15:48
  • I got it. Thanks for pointing that out. I'll update my answer. – sm925 Jan 04 '18 at 15:50
  • Please delete the incorrect stuff at the top rather than just adding a corrected version underneath. – Gregor Thomas Jan 04 '18 at 15:55
  • Removed previous thing. – sm925 Jan 04 '18 at 15:57
  • 1
    Removed my downvote. – Gregor Thomas Jan 04 '18 at 16:08