Learn R Programming

crayon (version 1.5.3)

col_substring: Substring(s) of an ANSI colored string

Description

This is the color-aware counterpart of base::substring(). It works exactly like the original, but keeps the colors in the substrings. The ANSI escape sequences are ignored when calculating the positions within the string.

Usage

col_substring(text, first, last = 1000000L)

Value

Character vector of the same length as x, containing the requested substrings. ANSI styles are retained.

Arguments

text

Character vector, potentially ANSI styled, or a vector to coarced to character. It is recycled to the longest of first and last.

first

Starting index or indices, recycled to match the length of x.

last

Ending index or indices, recycled to match the length of x.

See Also

Other ANSI string operations: col_align(), col_nchar(), col_strsplit(), col_substr()

Examples

Run this code
str <- paste(
  red("red"),
  "default",
  green("green")
)

cat(str, "\n")
cat(col_substring(str, 1, 5), "\n")
cat(col_substring(str, 1, 15), "\n")
cat(col_substring(str, 3, 7), "\n")

substring(strip_style(str), 1, 5)
substring(strip_style(str), 1, 15)
substring(strip_style(str), 3, 7)

str2 <- "another " %+%
  red("multi-", sep = "", underline("style")) %+%
  " text"

cat(str2, "\n")
cat(col_substring(str2, c(3,5), c(7, 18)), sep = "\n")
substring(strip_style(str2), c(3,5), c(7, 18))

Run the code above in your browser using DataLab