Replace first occurrence of “:” but not second in R

前端 未结 2 1031
我在风中等你
我在风中等你 2020-12-11 15:52

In order to be able to process I\'d like to replace the first occurrence of a : in a string (which is my marker, that a speech begins).

text &l         


        
相关标签:
2条回答
  • 2020-12-11 16:11

    You can also use str_replace from stringr package.

    text1 <- c("ABC:DEF:", "SDF", "::ASW")
    library(stringr)
    str_replace(text1, ":", "|")
    # [1] "ABC|DEF:" "SDF"      "|:ASW"  
    

    This replaces the first occurence of : with |.

    0 讨论(0)
  • 2020-12-11 16:30

    We can use sub as it matches only the first occurrence of pattern : and then we replace that with |.

    sub(':', '|', text)
    
    0 讨论(0)
提交回复
热议问题