【每天一个R语言命令】-nchar

【描述】

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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容