This is the color-aware counterpart of base::strsplit().
It works almost exactly like the original, but keeps the colors in the
substrings.
col_strsplit(x, split, ...)Character vector, potentially ANSI styled, or a vector to coarced to character.
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 if split has
zero characters, x is split into single characters.
Extra arguments are passed to base::strsplit().
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.
Other ANSI string operations:
col_align(),
col_nchar(),
col_substr(),
col_substring()
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"
#>