nchar2 {lazy.tools}R Documentation

Count the number of characters using bytes, not characters, as the unit.

Description

Count the number of characters using bytes, not characters, as the unit.

Usage

nchar2(x)

Arguments

x

A string

Details

This function is equivalent to native nchar function with type="byte" option.

About rawToChar and charToRaw functions:
To get the raw expression of character, use
raw <- charToRaw(paste(c(0:9," ",letters,LETTERS),collapse=""))
To get the numeric equivalent of raw, use
num <- as.numeric(raw) .
To recover the original characters, use
char <- rawToChar( as.raw(num) ) .

Value

A number or a numeric vector.

Examples

# A string consisting of single and multi byte characters
# The 3rd, 6th and 7th characters are uppercase Zenkaku Romans.
{if( regexpr("utf8",Sys.getlocale (category = "LC_CTYPE")) > 0 )
 nza <- c(97, 98, 239, 188, 163, 100, 101, 239, 188, 166, 239, 188, 167, 104)
else
 nza <- c(97,98,130,98,100,101,130,101,130,102,104)}
za <- rawToChar(as.raw(nza))
# za <- num2char(nza)

# location of the multi byte characters
nchar(za)
is.DBCS(za)
printm( nchar(za), length(charToRaw(za)), nchar2(za) )
printm(unlist(strsplit(za,"")),nbyte(za),is.DBCS(za))



[Package lazy.tools version 0.1.6 ]