这是一个错误吗?
> nchar(sprintf("%-20s", "Sao Paulo"))
[1] 20
> nchar(sprintf("%-20s", "São Paulo"))
[1] 19
> 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
答案 0 :(得分:3)
> nchar(sprintf("%-20s", "Sao Paulo"), type = "bytes")
[1] 20
> nchar(sprintf("%-20s", "São Paulo"), type = "bytes")
[1] 20
答案 1 :(得分:2)
如果您阅读了sprintf的帮助页面,它会谈到编码很重要的事实。 如果您查看nchar的帮助页面,您还会了解到有不同的类型。
因此,我看到以下内容(在Linux,R 3.3.0 beta上):
> nchars <- function(x) vapply(c("bytes","chars","width"),
function(typ) nchar(x, type=typ), 1)
> sp <- "São Paulo"
> Encoding(sp)
[1] "UTF-8"
> nchars(sp)
bytes chars width
10 9 9
> nchars(sprintf("%-20s", sp))
bytes chars width
20 19 19
>
所以我声称没有任何错误。 我不是说@TheRimalaya,而是在得出不同的结论