删除字符串的一部分 [英] Remove part of a string

查看:76
本文介绍了删除字符串的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何删除字符串的一部分?例如在 ATGAS_1121 中,我想删除 _ 之前的所有内容.

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

推荐答案

使用正则表达式.在这种情况下,您可以使用 gsub:

Use regular expressions. In this case, you can use gsub:

gsub("^.*?_","_","ATGAS_1121")
[1] "_1121"

此正则表达式匹配字符串的开头 (^)、任何重复零次或多次 (*) 的字符 (.) 和下划线 (_).这 ?使匹配懒惰",以便它只匹配第一个下划线.该匹配项仅替换为下划线.请参阅 ?regex 以获取更多详细信息和参考

This regular expression matches the beginning of the string (^), any character (.) repeated zero or more times (*), and underscore (_). The ? makes the match "lazy" so that it only matches are far as the first underscore. That match is replaced with just an underscore. See ?regex for more details and references

这篇关于删除字符串的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆