Description Usage Arguments Details See Also Examples
View source: R/str_collapse_whitespace.R
Collapses adjacent whitespace into a single character
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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")
)
|
string |
object to turn into a title case |
pattern |
character; one or more regular expression patterns for
matching whitespace characters. Defaults to the |
replacement |
character; the whitespace character to use for replacing. The default is "\1" – the character that matched. |
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"
?patterns
gsub
which is used to implement this function.
1 2 3 4 5 6 7 8 9 10 11 | 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_-]', " " )
str_collapse_whitespace( "A -B- C", '[\\s_\\-]', " " )
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.