strip {xpectr} | R Documentation |
Strip strings of non-alphanumeric characters
Description
Removes any character that is not alphanumeric or a space.
(Disabled by default): Remove numbers.
Reduces multiple consecutive whitespaces to a single whitespace and trims ends.
Can for instance be used to simplify error messages before checking them.
Usage
strip(
strings,
replacement = "",
remove_spaces = FALSE,
remove_numbers = FALSE,
remove_ansi = TRUE,
lowercase = FALSE,
allow_na = TRUE
)
Arguments
strings |
|
replacement |
What to replace blocks of punctuation with. (Character) |
remove_spaces |
Whether to remove all whitespaces. (Logical) |
remove_numbers |
Whether to remove all numbers. (Logical) |
remove_ansi |
Whether to remove ANSI control sequences. (Logical) |
lowercase |
Whether to make the strings lowercase. (Logical) |
allow_na |
Whether to allow |
Details
ANSI control sequences are removed with
fansi::strip_ctl()
.-
gsub("[^[:alnum:][:blank:]]", replacement, strings))
-
gsub('[0-9]+', '', strings)
(Note: only if specified!) -
trimws( gsub("[[:blank:]]+", " ", strings) )
(Or""
ifremove_spaces
isTRUE
)
Value
The stripped strings.
Author(s)
Ludvig Renbo Olsen, r-pkgs@ludvigolsen.dk
See Also
Other strippers:
strip_msg()
Examples
# Attach packages
library(xpectr)
strings <- c(
"Hello! I am George. \n\rDon't call me Frank! 123",
" \tAs that, is, not, my, name!"
)
strip(strings)
strip(strings, remove_spaces = TRUE)
strip(strings, remove_numbers = TRUE)