[Solved] Match a character in string and add characters before and after matched string using R [closed]


I modified the code to include the strings in single quotes. (R requires single or double quotes for strings. I used single quotes so as not to have to escape the double quotes.)

Before <- 'kzyFw4hw8EOC/655'
After <- 'kzyFw4hw8EOC"https://stackoverflow.com/"655'

Using base R:

gsub.method <- gsub("https://stackoverflow.com/", '"https://stackoverflow.com/"', Before)
gsub.method == After
# [1] TRUE

or using the package stringr from the tidydverse:

library(stringr)
stringr.method <- str_replace(Before, "https://stackoverflow.com/", '"https://stackoverflow.com/"')
stringr.method == After
# [1] TRUE

solved Match a character in string and add characters before and after matched string using R [closed]