The stringi and stringr packages let you trim whitespace, but
what if you want to trim something else from either (or both) side(s) of a
string? This function lets you select which pattern to trim and from which
side(s).
Arguments
- string
A character vector.
- pattern
The pattern to look for.
The default interpretation is a regular expression, as described in stringi::about_search_regex.
To match a without regular expression (i.e. as a human would), use coll(). For details see
stringr::regex().- side
Which side do you want to trim from?
"both"is the default, but you can also have just either"left"or"right"(or optionally the shortened"b","l"and"r").
See also
Other removers:
str_remove_quoted(),
str_singleize()
Examples
str_trim_anything("..abcd.", ".", "left")
#> [1] ""
str_trim_anything("..abcd.", coll("."), "left")
#> [1] "abcd."
str_trim_anything("-ghi--", "-", "both")
#> [1] "ghi"
str_trim_anything("-ghi--", "-")
#> [1] "ghi"
str_trim_anything("-ghi--", "-", "right")
#> [1] "-ghi"
str_trim_anything("-ghi--", "--")
#> [1] "-ghi"
str_trim_anything("-ghi--", "i-+")
#> [1] "-gh"
