【问题标题】:Using ascii in str_replace在 str_replace 中使用 ascii
【发布时间】:2021-01-20 08:12:48
【问题描述】:

我正在开发一个 R 包,该包具有替换 ü、ä 等字符的功能。 如果我检查包裹,我会收到警告:

便携包必须在其 R 代码中仅使用 ASCII 字符, 除了在 cmets 中。 对其他字符使用 \uxxxx 转义。

函数包含

  n <- str_replace_all(n, c('ü' = 'ue', 'ï' = 'ie', "ä" = 'ae','ö' = 'oe'))

我尝试将 ü 替换为“\u00fc”,其他的也一样。但这不起作用。

str_replace("uüe", \\u00cf, "ue")
str_replace("uüe", *\\u00cf", "ue")
str_replace("uüe", <+u00cf>, "ue")

任何想法,如何做到这一点?

【问题讨论】:

    标签: r tidyverse


    【解决方案1】:

    据我所知,您正在使用包stringr。这确实允许使用和操作非 ASCII 字符。

    例如:

    vec <- "ärmlich nicht über den wolken, höchstens hïmmlisch"
    

    设置要替换为setNames的字符串的名称:

    ref <- setNames(c("ue", "ie", "ae", "oe"),
                    c("ü", "ï", "ä", "ö"))
    

    将此集合输入str_replace_all操作:

    library(stringr)
    str_replace_all(vec, ref)
    [1] "aermlich nicht ueber den wolken, hoechstens hiemmlisch"
    

    【讨论】:

    • 虽然这是一个很好的紧凑的编写方式,但它不是我问题的解决方案。我想使用 ASCII 码,在函数中使用 ü,ä 等时消除警告,检查我的包时收到警告。
    猜你喜欢
    • 2011-07-10
    • 2017-01-01
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多