Learn R Programming

lettercase (version 0.13.1)

str_collapse_whitespace: Collpases multiple adjacent whitespace characters into one

Description

Collapses adjacent whitespace into a single character

Usage

str_collapse_whitespace(string, pattern = getOption("lettercase.whitespace", c(pattern_whitespace, pattern_whitespace_like)), replacement = getOption("lettercase.whitespace.replacement", "\\1"))
str_collapse_ws(string, pattern = getOption("lettercase.whitespace", c(pattern_whitespace, pattern_whitespace_like)), replacement = getOption("lettercase.whitespace.replacement", "\\1"))

Arguments

string
object to turn into a title case
pattern
character; one or more regular expression patterns for matching whitespace characters. Defaults to the lettercase.whitespace option or \s, -, _ if the option has not been set.
replacement
character; the whitespace character to use for replacing. The default is "\1" -- the character that matched.

Details

collapse_whitespace replaces repeated whitespace characters with a whitespace replacement. By default, it will find and replace any of multiple adjacent pattern characters with the replacement.

pattern can be a named patterns (see ?patterns) or character strings that are treated as regular expressions by default.

To collapse mixed whitespace, provide a single patterns in the form of a regular expression class, e.g. "[\s-_]". This can lead to confusing results if a back-reference is use as a replacement value. See note below.

See Also

?patterns gsub which is used to implement this function.

Examples

Run this code
str_collapse_whitespace( "A  B" )
  str_collapse_whitespace( "A  B  C" )
  str_collapse_whitespace( "A__B__C" )
  str_collapse_whitespace( "A  B__C" )
  str_collapse_whitespace( "A _B_ C" )  # No transformation, no matches

  # See note above:
  str_collapse_whitespace( "A _B_ C", '[\\s-_]' ) # possibly ill-defined
  str_collapse_whitespace( "A _B_ C", c("\\s", "_") )
  str_collapse_whitespace( "A _B_ C", '[\\s-_]', " " )

Run the code above in your browser using DataLab