Replace multiple characters, by index, in a string quickly

前端 未结 3 2041
滥情空心
滥情空心 2021-01-25 00:30

I\'m trying to quickly replace multiple characters in a string with another character such as *

For example, I have a string such as:

string         


        
3条回答
  •  一向
    一向 (楼主)
    2021-01-25 00:58

    We can use substring

    v1 <- c(1, 4, 6, 9)
    for(i in seq_along(v1)) substring(string, v1[i], v1[i]) <- "*"
    #[1] "*bc*e*gh*j"
    

    As we are using stringi, another option is

    library(stringi)
    stri_sub_all(string, from = v1, length = 1) <- "*"
    string
    #[1] "*bc*e*gh*j"
    

提交回复
热议问题