cchar {lazy.tools} | R Documentation |
Starting and Ending Columns of Each Character in a Text File
cchar(x, from = NULL, to = NULL, last = TRUE)
x |
A string vector |
This function can be used to get the substring of a string
by specifying the starting and the ending columns.
It is assumed that the double or triple byte characters takes two columns
of a text file. (Zenkaku Character)
If x
contains a string consisting of single and multibyte characters,
ft <- cchar(x) substr( x, which(ft[,1] == start), To=which(ft[,2] == stop) )
will return the characters between columns start
and stop
.
When from
is not given, a list of matrices
FT[[i]]
where FTT[[i]][j,1]
and FTT[[i]][j,2]
,
resp, are the starting and ending columns of the j
-th character
of the i
-th string.
When the input is a string, FT[[1]]
will be returned.
When from
is given, a list of matrices
FTc[[i]]
corresponding to each of the strings where
FTc[[i]][j,1], FTc[[i]][j,2]
are, resp,
the starting and ending characters corresponding to
from[j]
and to[j]
of the i-th string.
# 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), nchar2(za), nchar(za)+sum(is.DBCS(za)) )
printm(1:nchar(za), unlist(strsplit(za,"")),nbyte(za),cchar(za))
# When input is a string vector
cchar(c(za,za))
# convert column position to character position
# from and to as column position
from <- c(1,2,3,5,6,7,9,11)
to <- c(1,2,4,5,6,8,10,11)
# character position corresponding to (from,to) to be used in substr(ing)
FTc <- cchar(za,from,to)[[1]]
# substrings
ssg <- substring(za,FTc[,1],FTc[,2])
# substrings using substr2 which can hadle column (from,to)
ss2 <- substr2(za,from,to)
za1 <- strsplit(za,"")[[1]] # to print each character
printm(za1, from, to, FTc, ssg,ss2)
# use of last option
cchar( za, 1:11, 1:11 )
cchar( za, 1:11, 1:11, last=0 )
cchar( za, 3,4 )
cchar( za, 3,5 )
cchar( za, 3,7 )
cchar( za, 3,7, last=0 )
# When input is a string vector
cchar( c(za,za), 1:11, 1:11, last=0 )