I have the string foo+1234+bar.txt
, and I'm trying to substitute foo
with baz
and remove +bar
:
baz+1234.txt
I think I should use gsub
gsub("foo*", "baz", "foo+1234+bar.txt")
but I don't know how to selectively keep some of the characters while removing others at the same time.
You can do this as chained operation. First remove '+bar'
then replace 'foo'
with 'baz'
.
string <- "foo+1234+bar.txt"
sub('foo', 'baz', fixed = TRUE, sub('+bar', '', string, fixed = TRUE))
#[1] "baz+1234.txt"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.