Replace character at certain location within string

前端 未结 2 1129
无人共我
无人共我 2020-12-18 21:03

Given a certain string, e.g., s = \"tesX123\", how can I replace a certain character at a certain location?

In this example, the character at position <

相关标签:
2条回答
  • 2020-12-18 21:20

    Try substr()

    substr(s, 4, 4) <- "t"
    > s
    #[1] "test123"
    
    0 讨论(0)
  • 2020-12-18 21:25

    We can use sub

    sub("(.{3}).", "\\1t", s)
    #[1] "test123"
    
    0 讨论(0)
提交回复
热议问题