gpt4 book ai didi

go - 如何在 golang 中处理(解码或删除无效的 Unicode 代码点)带有表情符号的字符串?

转载 作者:IT王子 更新时间:2023-10-29 01:40:46 29 4
gpt4 key购买 nike

示例字符串:

"\u0410\u043b\u0435\u043a\u0441\u0430\u043d\u0434\u0440\u044b! \n\u0421\u043f\u0430\u0441\u0438\u0431\u043e \ud83d\udcf8 link.ru \u0437\u0430 \n#hashtag  Русское слово, an English word"

没有这个 \ud83d\udcf8 我的函数运行良好:

func convertUnicode(text string) string {
s, err := strconv.Unquote(`"` + text + `"`)
if err != nil {
// Error.Printf("can't convert: %s | err: %s\n", text, err)
return text
}
return s
}

我的问题是如何检测包含此类条目的文本?以及如何将其转换为表情符号或如何从文本中删除?谢谢

最佳答案

好吧,可能没那么简单,因为 \ud83d\udcf8 都不是有效的代码点,而是 UTF-16 编码中使用的代理对来编码 \U0001F4F8。现在 strconv.Unquote 会给你两个代理部分,你必须自己组合它们。

  1. 像您一样使用 strconv.Unquote 取消引用。
  2. 为方便起见转换为 []rune。
  3. 使用 unicode/utf16.IsSurrogate 查找代理对。
  4. 将代理对与 unicode/utf16.DecodeRune 组合。
  5. 转换回字符串。

关于go - 如何在 golang 中处理(解码或删除无效的 Unicode 代码点)带有表情符号的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52879524/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com