如何获取特殊字符的原始数字HTML表示?

问题描述 投票:3回答:1

当我将"\xfc"键入R时,它会导致[1] "ü"。我不希望这样,我希望这导致[1] "\xfc"。我也不太明白为什么Encoding("\xfc")"latin1"虽然我将Code-> Saving中的设置更改为UTF-8。我想编写一个函数来替换"ü"之类的一些特殊字符,例如"\xfc",但我无法做到这一点:

> stringr::str_replace_all("Müller", "ü", "\xfc")
[1] "Müller"
> stringr::str_replace_all("Müller", "ü", "\\xfc")
[1] "Mxfcller"
> stringr::str_replace_all("Müller", "ü", "\\\xfc")
[1] "Müller"
> stringr::str_replace_all("Müller", "ü", "\\\\xfc")
[1] "M\\xfcller"

我真正想要的是[1] "M\xfcller"

(怎么样)我可以实现这个目标吗?

r regex encoding special-characters
1个回答
0
投票

最后一行给出了您想要的结果。打印字符串时,反斜杠会被转义。为了看到这一点,让我们将字符串保存到文件,然后查看文件的内容。


s <- stringr::str_replace_all("Müller", "ü", "\\\\xfc")

writeLines(s, "test.txt")

cat(readLines("test.txt"))
#> M\xfcller

reprex package创建于2019-03-27(v0.2.1)

另请参阅此GitHub问题:https://github.com/STAT545-UBC/Discussion/issues/394

© www.soinside.com 2019 - 2024. All rights reserved.