This is the color-aware counterpart of base::strsplit()
.
It works almost exactly like the original, but keeps the colors in the
substrings.
Arguments
- x
Character vector, potentially ANSI styled, or a vector to coarced to character.
- split
Character vector of length 1 (or object which can be coerced to such) containing regular expression(s) (unless
fixed = TRUE
) to use for splitting. If empty matches occur, in particular ifsplit
has zero characters,x
is split into single characters.- ...
Extra arguments are passed to
base::strsplit()
.
Value
A list of the same length as x
, the \(i\)-th element of
which contains the vector of splits of x[i]
. ANSI styles are
retained.
See also
Other ANSI string operations:
col_align()
,
col_nchar()
,
col_substr()
,
col_substring()
Examples
str <- red("I am red---") %+%
green("and I am green-") %+%
underline("I underlined")
cat(str, "\n")
#> I am red---and I am green-I underlined
# split at dashes, keep color
cat(col_strsplit(str, "[-]+")[[1]], sep = "\n")
#> I am red
#> and I am green
#> I underlined
strsplit(strip_style(str), "[-]+")
#> [[1]]
#> [1] "I am red" "and I am green" "I underlined"
#>
# split to characters, keep color
cat(col_strsplit(str, "")[[1]], "\n", sep = " ")
#> I a m r e d - - - a n d I a m g r e e n - I u n d e r l i n e d
strsplit(strip_style(str), "")
#> [[1]]
#> [1] "I" " " "a" "m" " " "r" "e" "d" "-" "-" "-" "a" "n" "d" " " "I" " " "a" "m"
#> [20] " " "g" "r" "e" "e" "n" "-" "I" " " "u" "n" "d" "e" "r" "l" "i" "n" "e" "d"
#>