Brian Smith
Brian Smith

Reputation: 1353

Replacing Backward slash in R

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

Upvotes: 0

Views: 641

Answers (3)

manro
manro

Reputation: 3677

Try this:

str_replace("\\asd", fixed("\\"), "//")

Upvotes: 3

norie
norie

Reputation: 9857

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

> stringr::str_replace("\\asd", "\\\\", "//")
[1] "//asd"

Upvotes: 1

Ran Turner
Ran Turner

Reputation: 18116

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)

Upvotes: 1

Related Questions