How to compare and convert emoji characters in C#

二次信任 提交于 2019-12-03 13:40:01

You already got past the hard part. All you were missing is parsing the value in the array, and combining the 2 unicode characters before performing the check.

Here is a sample program that should work:

static void Main(string[] args)
{
    const string comparisonStr = "bicyclist: \U0001F6B4, and US flag: \U0001F1FA\U0001F1F8"; //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}!");
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!