Learn R Programming

cli (version 3.6.3)

ansi_nchar: Count number of characters in an ANSI colored string

Description

This is a color-aware counterpart of utf8_nchar(). By default it counts Unicode grapheme clusters, instead of code points.

Usage

ansi_nchar(x, type = c("chars", "bytes", "width", "graphemes", "codepoints"))

Value

Numeric vector, the length of the strings in the character vector.

Arguments

x

Character vector, potentially ANSI styled, or a vector to be coerced to character. If it converted to UTF-8.

type

Whether to count graphemes (characters), code points, bytes, or calculate the display width of the string.

See Also

Other ANSI string operations: ansi_align(), ansi_columns(), ansi_strsplit(), ansi_strtrim(), ansi_strwrap(), ansi_substring(), ansi_substr(), ansi_toupper(), ansi_trimws()

Examples

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

cat(str, "\n")
nchar(str)
ansi_nchar(str)
nchar(ansi_strip(str))

Run the code above in your browser using DataLab