如果它們存在于字串的末尾,我想洗掉最后的字符“txt”。但是,有時也可以在字串的其他位置找到這些字符。
即我想洗掉txt但"/some-text-here/txt"不洗掉,"/txt-not-to-remove-text/"因為在最后一個示例中它不會在最后一次出現之后發生/
所以,我只想提取txt它是否出現在字串的末尾。
string = c("/some-text-here/txt",
"/some-other-text-here/txt",
"/txt-not-to-remove-text/",
"/txt-another-line-of-text/txt")
uj5u.com熱心網友回復:
我們可能會使用trimws
trimws(string, whitespace = "/txt", which = 'right')
-輸出
[1] "/some-text-here" "/some-other-text-here"
[3] "/txt-not-to-remove-text/" "/txt-another-line-of-text"
或者使用sub并指定$字串的結尾()
sub("/txt$", "", string)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/460981.html
