Igor Chubin Igor Chubin -4 years ago 94
R Question

How to find the "real" number of characters in a Unicode string in R

I know how to find the length of a not Unicode string in R.


(thanks everyone who answered the question here: How to find the length of a string in R? ).

But what about Unicode strings?

How to find the length of a string (number of characters in a string) in a Unicode strings? How do I find the length (in bytes) and the number of characters (runes, symbols) in a Unicode string in R?

Answer Source

You can use nchar for the number of characters and for the number of bytes:

nchar("bi\u00dfchen", type="chars")
#[1] 7
nchar("bi\u00dfchen", type="bytes")
#[1] 8

Indeed, in the help, you can find details about how to compute the string size:

The ‘size’ of a character string can be measured in one of three ways (corresponding to the type argument):
The number of bytes needed to store the string (plus in C a final terminator which is not counted). chars
The number of human-readable characters.
The number of columns cat will use to print the string in a monospaced font. The same as chars if this cannot be calculated.

If you want to know the number of "symbols" inside the string that may (or may not) contain unicode (i.e. without interpreting the unicode symbol), you can use function stri_escape_unicode from package stringi:

nchar(stri_escape_unicode("bi\u00dfchen")) # same as stri_length(stri_escape_unicode("bi\u00dfchen"))
# [1] 12
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download