substr2 {lazy.utf8} | R Documentation |
Substring Using Column Location as the Unit.
substr2(x, from, to = 0, strict = 0, trim = 0, ...)
x |
A string vector |
from |
The first column (location in a line of a text file) |
to |
The last column or 0 meaning the end of string |
strict |
= 1 to check if start and stop are valid. |
trim |
= 1 to trim the result by lazy.tools::trim |
... |
options for lazy.tools::trim |
It is assumed that the double or triple byte character takes two colums
of a text file. (Zenkaku Character)
See the description of cchar
.
When scrict=1, if the start or stop value is invalid, NA will be returned.
When strict=0, if the start value is valid and the stop value is invalid,
stop will be set to the location of the last character.
When trim is used, the result will be re-encoded to UTF-8.
A vector consisting of the substrings of the input vector. The result will be a list if the input is a string vector.
# 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) printm(za) # location of the multi byte characters is.DBCS(za) printm( nchar(za), nchar2(za), nchar(za)+sum(is.DBCS(za)) ) printm(unlist(strsplit(za,"")),nbyte(za),cchar(za)) # The first to the fourth characters substr(za,1,4) # The characters of za in the first to the fourth column. substr2(za,1,4) # Another example {if( regexpr("utf8",Sys.getlocale (category = "LC_CTYPE")) > 0 ) nn <- c(87, 101, 32, 104, 97, 118, 101, 32, 228, 184, 141, 232, 184 , 176, 231, 159, 169, 32, 121, 101, 116, 33) else nn <- c(87, 101, 32, 104, 97, 118, 101, 32, 149, 115, 230, 250, 139 , 233, 32, 121, 101, 116, 33)} za2=num2char(nn) printm( nchar(za2), nchar2(za2), nchar(za2)+sum(is.DBCS(za2)) ) printm(1:nchar(za2),unlist(strsplit(za2,"")),nbyte(za2),cchar(za2)) # Note that start and stop must be the numbers contained in, resp, # the first and the second columns of the returning matrix from # cchar function. substr2(za2,4,7) # ok substr2(za2,4,9) # NG substr2(za2,4,10) # ok substr2(za2,4,11) # NG substr2(za2,4,12) # ok substr2(za2,4,9, strict=1) # NG substr2(za2,4,11, strict=1) # NG substr2(za2,9,10, strict=1) # ok substr2(za2,9,11, strict=1) # NG # reading Shift-JIS files on from R.4.1.3 nobs <- 10 infile1 <- system.file("TestData1.txt",package="lazy.utf8") if( regexpr("utf8",Sys.getlocale (category = "LC_CTYPE")) > 0 ){ temp1 <- ReadLines(infile1, n=nobs+2)[-(1:2)] } else{ # The result will be Double Byte. temp1 <- ReadLines(infile1, n=nobs+2, asDB=1)[-(1:2)] # The result will be Triple Byte. temp11 <- ReadLines(infile1, n=nobs+2)[-(1:2)] nbyte(temp1) nbyte(temp11) identical(temp1[-nobs],temp11[-nobs]) # true identical(nbyte(temp1[-nobs]),nbyte(temp11[-nobs])) # false } # reading UTF-8 nobs <- 10 infile2 <- system.file("TestData2.txt",package="lazy.utf8") temp2 <- ReadLines(infile2, n=nobs+2, encoding="UTF-8")[-(1:2)] nbyte(temp2) # starting and ending columns of each field from=c(1, 7,25,26,27,28,29,30,31,32,34) to=c( 5,23,25,26,27,28,29,30,31,32,39) # character locations corresponding to from and to: should be equal. FTc1=cchar( temp1, from, to ) FTc2=cchar( temp2, from, to ) char=strsplit(temp1[1],"")[[1]] printm( char, from,to, FTc1[[1]],FTc2[[1]] ) # substringing using FTc substring( temp2[1], FTc1[[1]][,1], FTc2[[1]][,2] ) substring( temp2[2], FTc1[[2]][,1], FTc2[[2]][,2] ) # get the field values as substrings using substr2 function s12=substr2(temp1,from[2],to[2]) s14=substr2(temp1,from[4],to[4]) s111=substr2(temp1,from[11],to[11]) s22=substr2(temp2,from[2],to[2]) s24=substr2(temp2,from[4],to[4]) s211=substr2(temp2,from[11],to[11]) # comparison printm(s14,s24) # Note that s111 and s211 are trimmed because they come from # the variable length last columns. # (On R4.1.3 s21 and s11 cannot be printmed together by printm.) print.listof(list(s12=s12,s22=s22,s111=s111,s211=s211)) # On R4.1.3, # substr or substr2 does not change the encoding. # lazy.tools::trim changes the encoding. # s12t=trim(s12) # printm( s12[1], s12t[1] ) # printm( nbyte(s12[1]),nbyte(s12t[1]) ) # This does not. # s12tt=substr(s12,1,nchar(trim(s12))) # printm(s12[1],s12tt[1]) # printm(nbyte(s12[1]),nbyte(s12tt[1]))