【问题标题】:Coverting unicode codeepoint format转换 unicode 代码点格式
【发布时间】:2018-01-06 00:21:14
【问题描述】:

假设我有一个字符串,其中包含表示表情符号的字节:

string <- "This is a test. U+1F600"

我怎样才能把它变成

string <- "This is a test. \U0001F600"

这样我就可以把它渲染成

utf8_print("This is a test \U0001F600")
[1] "This is a test ????​"

【问题讨论】:

标签: r encoding character-encoding


【解决方案1】:

这是一种 hack,但它适用于您的情况:

string <- c("This is a test. U+1F600", "Another test")

# change U+XXXXYYYY to \UXXXXYYYY, quote and encode special characters
expr <- gsub("U[+]([0-9A-Fa-f]{1,8})", "\\\\U\\1",
             encodeString(string, quote = '"'))

# evaluate the string as an R expression
vapply(parse(text = expr, keep.source = FALSE), eval, "")
#> [1] "This is a test. \U0001f600" "Another test"

【讨论】:

  • 哇!太棒了。还有一个问题。我将如何渲染未以 unicode 表示但由 ¯\_(ツ)_/¯ 表示的耸肩表情符号?
  • 这似乎是一个无关的问题,您应该单独提出,以便其他人更容易找到答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-29
  • 2012-06-20
相关资源
最近更新 更多