Replaces every occurrence of pattern in string
with replacement. Supports both regular expressions and literal
string matching via fixed().
Arguments
- string
A character vector.
- pattern
A regular expression string, or a literal string wrapped in
fixed(), or a named character vector where names are regex patterns and values are replacements (applied sequentially).- replacement
A character string to replace each match with. Use
""to delete matches.
Examples
# Regex replacement
str_replace_all("hello world", "o", "0")
#> [1] "hell0 w0rld"
# Fixed (literal) replacement
str_replace_all("a.b.c", fixed("."), "-")
#> [1] "a-b-c"
# Remove all spaces
str_replace_all("remove all spaces", fixed(" "), "")
#> [1] "removeallspaces"
# Named vector: multiple replacements applied in order
str_replace_all("aabbcc", c("a" = "X", "b" = "Y"))
#> [1] "XXYYcc"