0

I want to replace backward slashes with forwards slashes in a string. So I used below syntax in R.

stringr::str_replace("\\", "//", "\\asd")

However it fails to replace the backward slashes in the given string.

Could you please help to find the right way to replace them?

I am using R in Windows 10 machine

Brian Smith
  • 1,200
  • 4
  • 16

3 Answers3

2

Try this:

str_replace("\\asd", fixed("\\"), "//")
manro
  • 3,529
  • 2
  • 9
  • 22
1

You could use gsub function in R which is used for replacement operations. The functions takes the input and substitutes it against the specified value.

gsub("\\\\", "/", x)
Ran Turner
  • 14,906
  • 5
  • 47
  • 53
1

You have the arguments in the wrong order and you need to escape the backslashes.

> stringr::str_replace("\\asd", "\\\\", "//")
[1] "//asd"
norie
  • 9,609
  • 2
  • 11
  • 18