加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在C#中比较和转换表情符号字符

发布时间:2020-12-15 08:28:29 所属栏目:百科 来源:网络整理
导读:我试图弄清楚如何检查一个字符串是否包含一个specfic表情符号.例如,查看以下两个表情符号: 自行车:http://unicode.org/emoji/charts/full-emoji-list.html#1f6b4 美国国旗:http://unicode.org/emoji/charts/full-emoji-list.html#1f1fa_1f1f8 自行车手是U
我试图弄清楚如何检查一个字符串是否包含一个specfic表情符号.例如,查看以下两个表情符号:

自行车:http://unicode.org/emoji/charts/full-emoji-list.html#1f6b4

美国国旗:http://unicode.org/emoji/charts/full-emoji-list.html#1f1fa_1f1f8

自行车手是U 1F6B4,美国国旗是U 1F1FA U 1F1F8.

但是,要检查的表情符号是以这样的数组提供给我的,只有字符串中的数值:

var checkFor = new string[] {"1F6B4","1F1FA-1F1F8"};

如何将这些数组值转换为实际的unicode字符并检查字符串是否包含它们?

我可以为自行车骑士工作,但是对于美国国旗,我很难过.

对于自行车骑士,我正在做以下事情:

const string comparisonStr = "..."; //some string containing text and emoji

var hexVal = Convert.ToInt32(checkFor[0],16);
var strVal = Char.ConvertFromUtf32(hexVal);

//now I can successfully do the following check

var exists = comparisonStr.Contains(strVal);

但由于多个代码点,这对美国国旗不起作用.

解决方法

你已经超越了困难的部分.您所缺少的只是解析数组中的值,并在执行检查之前组合2个unicode字符.

这是一个应该工作的示例程序:

static void Main(string[] args)
{
    const string comparisonStr = "bicyclist: U0001F6B4,and US flag: U0001F1FAU0001F1F8"; //some string containing text and emoji
    var checkFor = new string[] { "1F6B4","1F1FA-1F1F8" };

    foreach (var searchStringInHex in checkFor)
    {
        string searchString = string.Join(string.Empty,searchStringInHex.Split('-')
                                                        .Select(hex => char.ConvertFromUtf32(Convert.ToInt32(hex,16))));

        if (comparisonStr.Contains(searchString))
        {
            Console.WriteLine($"Found {searchStringInHex}!");
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读