Given a character vector, each element is split, the first nchars characters are extracted from each part and all parts are concatenated with sep. This is useful to obtain shortcuts of lengthy strings.

str_to_shortcut(x, split, nchars = 1, sep = "-", ...)

Arguments

x

[character] Vector of strings.

split

[character(1)]
Character string used to split each component of x.

nchars

[integer(1)]
Length of extracted prefix for each part. Defaults to 1.

sep

[string]
Possibly empty glue string. Default is “-”.

...

[any]
Further arguments passed down to strsplit. The most important here is split.

Value

Shortcut version of x.

See also

Other string helpers: str_explode(), str_parse()

Examples

x = c("hello-world", "some-length-string") str_to_shortcut(x, split = "-")
#> [1] "h-w" "s-l-s"
str_to_shortcut(x, nchars = 2L, sep = "", split = "-")
#> [1] "hewo" "solest"