Remove part of a string

前端 未结 6 1813
执笔经年
执笔经年 2020-11-27 11:07

How do I remove part of a string? For example in ATGAS_1121 I want to remove everything before _.

6条回答
  •  没有蜡笔的小新
    2020-11-27 11:40

    Maybe the most intuitive solution is probably to use the stringr function str_remove which is even easier than str_replace as it has only 1 argument instead of 2.

    The only tricky part in your example is that you want to keep the underscore but its possible: You must match the regular expression until it finds the specified string pattern (?=pattern).

    See example:

    strings = c("TGAS_1121", "MGAS_1432", "ATGAS_1121")
    strings %>% stringr::str_remove(".+?(?=_)")
    
    [1] "_1121" "_1432" "_1121"
    

提交回复
热议问题