charClass {utils}R Documentation

Character Classification


An interface to the (C99) wide character classification functions in use.


charClass(x, class)



Either a UTF-8-encoded length-1 character vector or an integer vector of Unicode points (or a vector coercible to integer).


A character string, one of those given in the ‘Details’ section.


The classification into character classes is platform-dependent. The classes are determined by internal tables on Windows and (optionally but by default) on macOS and AIX.

The character classes are interpreted as follows:


Alphabetic or numeric.




Space or tab.


Control characters.


Digits 0-9.


Graphical characters (printable characters except whitespace).


Lower-case alphabetic.


Printable characters.


Punctuation characters. Some platforms treat all non-alphanumeric graphical characters as punctuation.


Whitespace, including tabs, form and line feeds and carriage returns. Some OSes include non-breaking spaces, some exclude them.


Upper-case alphabetic.


Hexadecimal character, one of 0-9A-fa-f.

Alphabetic characters contain all lower- and upper-case ones and some others (for example, those in ‘title case’).

Whether a character is printable is used to decide whether to escape it when printing – see the help for print.default.

If x is a character string it should either be ASCII or declared as UTF-8 – see Encoding.

charClass was added in R 4.1.0. A less direct way to examine character classes which also worked in earlier versions is to use something like grepl("[[:print:]]", intToUtf8(x)) – however, the regular-expression code might not use the same classification functions as printing and on macOS used not to.


A logical vector of the length the number of characters or integers in x.


Non-ASCII digits are excluded by the C99 standard from the class "digit": most platforms will have them as alphabetic.

It is an assumption that the system's wide character classification functions are coded in Unicode points, but this is known to be true for all recent platforms.

The classification may depend on the locale even on one platform.

See Also

Character classes are used in regular expressions.

The OS's man pages for iswctype and wctype.


x <- c(48:70, 32, 0xa0) # Last is non-breaking space
cl <- c("alnum", "alpha", "blank", "digit", "graph", "punct", "upper", "xdigit")
X <- lapply(cl, function(y) charClass(x,y)); names(X) <- cl
X <-; row.names(X) <- sQuote(intToUtf8(x, multiple = TRUE))

charClass("ABC123", "alpha")
## Some accented capital Greek characters
(x <- "\u0386\u0388\u0389")
charClass(x, "upper")

## How many printable characters are there? (Around 280,000 in Unicode 13.)
## There are 2^21-1 possible Unicode points (most not yet assigned).
pr <- charClass(1:0x1fffff, "print") 

[Package utils version 4.4.1 Index]