【描述】
nchar takes a character vector as an argument and returns a vector whose elements contain the sizes of the corresponding elements of x.
nchar计算x的字符数
【用法】
nchar(x, type = "chars", allowNA = FALSE, keepNA = NA)
nzchar(x, keepNA = FALSE)
【参数】
x
character vector, or a vector to be coerced to a character vector. Giving a factor is an error.
type
character string: partial matching to one of c("bytes", "chars", "width"). See ‘Details’.
allowNA
logical: should NA be returned for invalid multibyte strings or "bytes"-encoded strings (rather than throwing an error)?
keepNA
logical: should NA be returned where ever x is NA? If false, nchar() returns 2, as that is the number of printing characters used when strings are written to output, and nzchar() is TRUE. The default for nchar(), NA, means to use keepNA = TRUE unless type is "width". Used to be (implicitly) hard coded to FALSE in R versions <= 3.2.0.
【代码】
> nchar('I love R')
[1] 8
> nchar('R语言')
[1] 3
注意:nchar与length的区别在于length计算的字符串个数,如
> nchar('I love R')
[1] 8
> length('I love R')
[1] 1
网友评论