Replace multiple characters, by index, in a string quickly

前端 未结 3 2078
滥情空心
滥情空心 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 01:09

    in base R, besides the method of substring() and for-loop shown by @akrun,, you can use utf8ToInt() and intToUtf8 to make it

    v <- utf8ToInt(string)
    v[string_indexes_replaced ] <- utf8ToInt("*")
    res <- intToUtf8(v)
    

    which gives

    > res
    [1] "*bc*e*gh*j"
    

提交回复
热议问题