String / text processing functions

trim(x)

chomp(x)

text_wrap(x, n = 20L)

translit(x, ...)

rand_string(n)

Arguments

x

a character vector

n

an integer of length 1. text_wrap() will silently coerce any numeric value passed to n to an integer.

...

addtional parameters used by specific functions. See Details.

Details

  • trimreturns a version of x without trailing OR leading spaces.

  • chompreturns a version of x without trailing newline delimiters.

  • text_wrapreturns a version of x in which each element substitutes newline delimiters for the immediate next space (e.g. ' ') after every nth character.

  • translitreturns a version of x recoded to ASCII, using iconv and //TRANSLIT. Parameters in ... are passed directly to iconv.

  • rand_stringreturns a randomly generated alphanumeric string of length n.

References

I attribute the regular expression that powers text_wrap() to StackOverflow user xiechao: http://stackoverflow.com/a/2352006/3277821

Examples

trim("foo ")
#> [1] "foo"
trim(" bar ")
#> [1] "bar"
trim("baz")
#> [1] "baz"
chomp("foo\n")
#> [1] "foo"
chomp("bar")
#> [1] "bar"
s <- "The quick brown fox jumped over the lazy dog." text_wrap(s)
#> [1] "The quick brown fox\njumped over the lazy\ndog."
text_wrap(s, 10)
#> [1] "The quick\nbrown fox\njumped\nover the\nlazy dog."
cat(paste0(text_wrap(s), "\n"))
#> The quick brown fox #> jumped over the lazy #> dog.
rand_string()
#> [1] "FwWKN1Jp41pyVhmkP15IJQpc4e9Lk4cKo4YRnTnCuJpC48Y3KV9SnsS1oa6BUP"
rand_string(5)
#> [1] "zvTPf"