strip: Strip strings of non-alphanumeric characters

View source: R/strip.R

stripR Documentation

Strip strings of non-alphanumeric characters

Description

\Sexpr[results=rd, stage=render]{lifecycle::badge("experimental")}
  1. Removes any character that is not alphanumeric or a space.

  2. (Disabled by default): Remove numbers.

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

vector of strings. (Character)

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 strings to contain NAs. (Logical)

Details

  1. ANSI control sequences are removed with fansi::strip_ctl().

  2. gsub("[^[:alnum:][:blank:]]", replacement, strings))

  3. gsub('[0-9]+', '', strings) (Note: only if specified!)

  4. trimws( gsub("[[:blank:]]+", " ", strings) ) (Or "" if remove_spaces is TRUE)

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)

xpectr documentation built on Nov. 18, 2022, 5:10 p.m.