【问题标题】:How to find the "real" number of characters in a Unicode string in R如何在R中的Unicode字符串中找到“真实”字符数
【发布时间】:2017-07-17 20:02:06
【问题描述】:

我知道如何在 R 中找到非 Unicode 字符串的长度。

nchar("ABC")

(感谢所有在这里回答问题的人:How to find the length of a string in R?)。

但是 Unicode 字符串呢?

如何在 Unicode 字符串中查找字符串的长度(字符串中的字符数)?如何在 R 中找到 Unicode 字符串的长度(以字节为单位)和字符数(符文、符号)?

【问题讨论】:

    标签: r string unicode string-length


    【解决方案1】:

    characters 的号码和bytes 的号码可以使用nchar

    nchar("bi\u00dfchen", type="chars")
    #[1] 7
    nchar("bi\u00dfchen", type="bytes")
    #[1] 8
    

    确实,在帮助中,您可以找到有关如何计算字符串大小的详细信息:

    字符串的“大小”可以通过以下三种方式之一来衡量(对应于类型参数):

    bytes:存储字符串所需的字节数(在 C 中加上一个不计入的最终终止符)。

    chars:人类可读的字符数。

    width:cat 用于以等宽字体打印字符串的列数。如果无法计算,则与 chars 相同。

    如果您想知道字符串中可能(或可能不)包含 unicode 的“符号”的数量(即没有解释 unicode 符号),您可以使用函数stri_escape_unicode from包stringi:

    library(stringi)
    nchar(stri_escape_unicode("bi\u00dfchen")) # same as stri_length(stri_escape_unicode("bi\u00dfchen"))
    # [1] 12
    

    【讨论】:

      猜你喜欢
      • 2018-11-24
      • 1970-01-01
      • 1970-01-01
      • 2011-04-02
      • 2010-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-12
      相关资源
      最近更新 更多