To replace a character at a specific index in a string in R, you can use string manipulation functions like substr()
.
In this example,
str
.substr()
function to replace the character at index index
with the character newChar
.str
.str <- "Hello World"
index <- 7
newChar <- "x"
str <- paste0(substr(str, 1, index - 1), newChar, substr(str, index + 1, nchar(str)))
print(str)
[1] "Hello xorld"
In this tutorial, we learned How to Replace Character at Specific Index in String in R language with well detailed examples.