gpt4 book ai didi

c# 将带有表情符号的字符串转换为 unicode

转载 作者:行者123 更新时间:2023-11-30 16:41:20 25 4
gpt4 key购买 nike

我从客户端获取这样的字符串:

This is a face  :grin: 

我需要将 :grin: 转换为 unicode 以便将其发送到其他服务。

Any clue how to do that?

最佳答案

这是一个link到一个包含相关信息的非常好的 json 文件。它包含带有表情符号的巨大数组(大约 1500 个条目),我们对 2 个属性感兴趣:“short_name”代表名称,如“grin”,以及“unified”属性,它包含 unicode 表示,如“1F601”。

我构建了一个辅助类来用它们的 unicode 等价物替换像“:grin:”这样的短名称:

public static class EmojiParser {
static readonly Dictionary<string, string> _colonedEmojis;
static readonly Regex _colonedRegex;
static EmojiParser() {
// load mentioned json from somewhere
var data = JArray.Parse(File.ReadAllText(@"C:\path\to\emoji.json"));
_colonedEmojis = data.OfType<JObject>().ToDictionary(
// key dictionary by coloned short names
c => ":" + ((JValue)c["short_name"]).Value.ToString() + ":",
c => {
var unicodeRaw = ((JValue)c["unified"]).Value.ToString();
var chars = new List<char>();
// some characters are multibyte in UTF32, split them
foreach (var point in unicodeRaw.Split('-'))
{
// parse hex to 32-bit unsigned integer (UTF32)
uint unicodeInt = uint.Parse(point, System.Globalization.NumberStyles.HexNumber);
// convert to bytes and get chars with UTF32 encoding
chars.AddRange(Encoding.UTF32.GetChars(BitConverter.GetBytes(unicodeInt)));
}
// this is resulting emoji
return new string(chars.ToArray());
});
// build huge regex (all 1500 emojies combined) by join all names with OR ("|")
_colonedRegex = new Regex(String.Join("|", _colonedEmojis.Keys.Select(Regex.Escape)));
}

public static string ReplaceColonNames(string input) {
// replace match using dictoinary
return _colonedRegex.Replace(input, match => _colonedEmojis[match.Value]);
}
}

用法很明显:

var target = "This is a face&nbsp;&nbsp;:grin:&nbsp;:hash:";
target = EmojiParser.ReplaceColonNames(target);

它非常快(第一次运行除外,因为静态构造函数初始化)。在你的弦上它需要不到 1 毫秒(无法用秒表测量,总是显示 0 毫秒)。在实践中您永远不会遇到的巨大字符串(1MB 文本)在我的机器上需要 300 毫秒。

关于c# 将带有表情符号的字符串转换为 unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49375596/

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