Skip to content

Instantly share code, notes, and snippets.

@JosiahParry
Created October 1, 2024 20:05
Show Gist options
  • Save JosiahParry/7d953f4af6848e7012566f7abfc63473 to your computer and use it in GitHub Desktop.
Save JosiahParry/7d953f4af6848e7012566f7abfc63473 to your computer and use it in GitHub Desktop.
str_replace_many <- function(string, ...) {
replacements <- rlang::list2(...)
patterns <- rlang::names2(replacements)
if (anyNA(patterns) || any(!nzchar(patterns))) {
cli::cli_abort("All arguments passed to {.arg ...} must be named")
}
for (pattern in replacements) {
rlang:::check_string(pattern, arg = pattern)
}
stringr::str_replace_all(string, unlist(replacements))
}
str_replace_many("del hello wor!d", "del " = "", "!" = "l")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment