Function that replaces a specified pattern found within a string (or vector of strings) with a user-specified pattern.

replace_string(s, to_replace, replace_with = "")

Arguments

s

A character vector.

to_replace

A character vector, the patterns to match and replace within each string.

replace_with

An optional character vector, either of length one or of matching length to to_replace, the patterns to substitute.

Value

A character string.

Examples

# Example string
x <- c( 'AA', 'AB', 'AC', 'DD' )
# Remove the letter 'A'
replace_string( x, 'A' )
#> [1] ""   "B"  "C"  "DD"
# Replace the letter 'A' with '1'
replace_string( x, 'A', '1' )
#> [1] "11" "1B" "1C" "DD"
# Replace multiple letters
replace_string( x, c( 'B', 'C' ), c( '1', '2' ) )
#> [1] "AA" "A1" "A2" "DD"