data_string <- "Substring removing"
# $ pins the match to the end
no_suffix <- sub(" removing$", "", data_string)
# no_suffix = Substring
# ^ pins it to the start
no_prefix <- sub("^Sub", "", no_suffix)
# no_prefix = string
# From the middle
marked <- "Substring removing!"
middle <- sub(" removing", "", marked,
fixed = TRUE)
# middle = Substring!
# Without $ the first match goes, not the last
text <- "x removing y removing"
first_gone <- sub(" removing", "", text,
fixed = TRUE)
# first_gone = x y removing
last_gone <- sub(" removing$", "", text)
# last_gone = x removing y
# gsub() removes every match
all_gone <- gsub(" removing", "", text,
fixed = TRUE)
# all_gone = x y
cat("no_suffix =", no_suffix, "\n")
cat("no_prefix =", no_prefix, "\n")
cat("middle =", middle, "\n")
cat("first_gone =", first_gone, "\n")
cat("last_gone =", last_gone, "\n")
cat("all_gone =", all_gone, "\n")