Is there an elegant way to remove a sub-string within a string based on the index of the characters?
Here is how I do it now:
# My data
mystring <- "Hello, how are {you} doing?"
index_of_substring <- c(16,20)
# Pasting two substrings
mystring_no_substring <- paste0(substr(mystring, 1, index_of_substring[1]-1), substr(mystring, index_of_substring[2]+1, nchar(mystring)))
# Cleaning extra spaces
mystring_no_substring <- gsub(" ", " ", mystring_no_substring)
I could of course write this up to a general function, but I was just wondering if there was an elegant solution out there, e.g. to substitute an index in a string with nothing or another word.
Note: This is not a regex question.
stringi::stri_sub(mystring, 16, 20) <- ""for example?stringr::str_sub(mystring, 16, 20) <- ""