String / text processing functions
trim(x) chomp(x) text_wrap(x, n = 20L) translit(x, ...) rand_string(n)
x | a character vector |
---|---|
n | an integer of length 1. |
... | addtional parameters used by specific functions. See Details. |
trim
returns a version of x
without trailing OR leading spaces.
chomp
returns a version of x
without trailing newline delimiters.
text_wrap
returns a version of x in which each element substitutes newline
delimiters for the immediate next space (e.g. ' ') after every n
th character.
translit
returns a version of x
recoded to ASCII, using iconv
and
//TRANSLIT
. Parameters in ...
are passed directly to iconv
.
rand_string
returns a randomly generated alphanumeric string of length n
.
I attribute the regular expression that powers text_wrap()
to StackOverflow user xiechao:
http://stackoverflow.com/a/2352006/3277821
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"