strex (version 1.2.0)

str_trim_anything: Trim something other than whitespace

Description

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).

Usage

str_trim_anything(string, pattern, side = "both")

Arguments

string

A character vector.

pattern

The pattern to look for.

The default interpretation is a regular expression, as described in stringi::stringi-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").

Value

A string.

See Also

Other removers: str_remove_quoted, str_singleize

Examples

Run this code
# NOT RUN {
str_trim_anything("..abcd.", ".", "left")
str_trim_anything("..abcd.", coll("."), "left")
str_trim_anything("-ghi--", "-", "both")
str_trim_anything("-ghi--", "-")
str_trim_anything("-ghi--", "-", "right")
str_trim_anything("-ghi--", "--")
str_trim_anything("-ghi--", "i-+")
# }

Run the code above in your browser using DataCamp Workspace