str_collapse_whitespace: Collpases multiple adjacent whitespace characters into one

Description Usage Arguments Details Note See Also Examples

Description

Collapses adjacent whitespace into a single character

Usage

1
2
3
4
5
6
7
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.

Note

It is inadvisable to have pattern be a multiple-character regex class, e.g. "[\s_]" while also using a back reference replacement value, e.g. "\1". This leads to confusing results.

> str_collapse_whitespace( "A _B_ C", '[\s-_]', "\1" ) [1] "A_B C"

The solution is to either provide a character vector for a {pattern} or not use back-references for the replacement depending on the desired outcome.

> str_collapse_whitespace( "A _B_ C", c("\s", "_") ) [1] "A _B_ C" > str_collapse_whitespace( "A _B_ C", '[\s-_]', " " ) [1] "A B C"

See Also

?patterns
gsub which is used to implement this function.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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-_]', " " )

lettercase documentation built on May 1, 2019, 9:45 p.m.