这是一个错误吗?
> nchar(sprintf("%-20s", "Sao Paulo"))
[1] 20
> nchar(sprintf("%-20s", "São Paulo"))
[1] 19
Run Code Online (Sandbox Code Playgroud)
> sessionInfo()
R version 3.2.4 (2016-03-10)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.4 (El Capitan)
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_3.2.4 fortunes_1.5-2
Run Code Online (Sandbox Code Playgroud)
如果您阅读 sprintf 的帮助页面,它会谈到编码很重要这一事实。\n如果您查看 nchar 的帮助页面,您还会了解到有不同的类型。
\n\n结果,我看到以下内容(在 Linux,R 3.3.0 beta 上):
\n\n> nchars <- function(x) vapply(c("bytes","chars","width"),\n function(typ) nchar(x, type=typ), 1)\n> sp <- "S\xc3\xa3o Paulo"\n> Encoding(sp)\n[1] "UTF-8"\n> nchars(sp)\nbytes chars width \n 10 9 9 \n> nchars(sprintf("%-20s", sp))\nbytes chars width \n 20 19 19 \n> \nRun Code Online (Sandbox Code Playgroud)\n\n所以我声称根本没有错误。\n我所说的并不比@TheRimalaya多,但我得出了不同的结论
\n