如何将包含 Unicode 快捷方式的字符串转换为 R 中的 TRUE Unicode 字符?
How to Convert String contains Unicode Shortcut into TRUE Unicode character in R?
我有这个字符串包含表情符号表示的特定字符的快捷方式:
x <- "\U0001F48C"
如何将其转换为 True Unicode(使用串联),等同于:
y <- "\U0001F48C"
cat 是检查此 Unicode 的便捷工具(至少这是我所知道的)
cat(y)
但是我想构建一些函数来传递最后一个特定的唯一编码,例如在这种情况下
converter <- function(last_unicode_label="1F48C"){
#convert as equivalent as y
}
编码列表可以在这里找到:https://apps.timwhitlock.info/emoji/tables/unicode
有多种方法可以做到这一点。也许最简单的方法是将字符串的十六进制部分转换为整数并使用 intToUtf8
from base R:
mystr <- c("\U0001F48C", "\U0001F48D")
mystr
#> [1] "\U0001F48C" "\U0001F48D"
mystr <- unlist(lapply(as.list(gsub("\\U", "0x", mystr)), intToUtf8))
mystr
#> [1] "\U0001f48c" "\U0001f48d"
最好用一个小实用函数代替:
unescape <- function(x) unlist(lapply(as.list(gsub("\\U", "0x", x)), intToUtf8))
我有这个字符串包含表情符号表示的特定字符的快捷方式:
x <- "\U0001F48C"
如何将其转换为 True Unicode(使用串联),等同于:
y <- "\U0001F48C"
cat 是检查此 Unicode 的便捷工具(至少这是我所知道的)
cat(y)
但是我想构建一些函数来传递最后一个特定的唯一编码,例如在这种情况下
converter <- function(last_unicode_label="1F48C"){
#convert as equivalent as y
}
编码列表可以在这里找到:https://apps.timwhitlock.info/emoji/tables/unicode
有多种方法可以做到这一点。也许最简单的方法是将字符串的十六进制部分转换为整数并使用 intToUtf8
from base R:
mystr <- c("\U0001F48C", "\U0001F48D")
mystr
#> [1] "\U0001F48C" "\U0001F48D"
mystr <- unlist(lapply(as.list(gsub("\\U", "0x", mystr)), intToUtf8))
mystr
#> [1] "\U0001f48c" "\U0001f48d"
最好用一个小实用函数代替:
unescape <- function(x) unlist(lapply(as.list(gsub("\\U", "0x", x)), intToUtf8))