How to compare and convert emoji characters in C #

I am trying to figure out how to check if a string contains specfic emoji. For example, look at the following two emoji:

Bicyclist: http://unicode.org/emoji/charts/full-emoji-list.html#1f6b4

USA flag: http://unicode.org/emoji/charts/full-emoji-list.html#1f1fa_1f1f8

Bicyclist U+1F6B4 , and the US flag U+1F1FA U+1F1F8 .

However, emoji for verification is provided to me in an array like this, with only a numeric value in the lines:

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

How can I convert these array values ​​to actual Unicode characters and check if the string contains them?

I can get something that works for a Bicyclist, but for the US flag, I'm at a standstill.

For Bicyclist, I do the following:

 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); 

But this will not work with the US flag due to the many code points.

+6
source share
1 answer

You have already gone through the hard part. All you were missing was to parse the value in the array and combine the two unicode characters before performing the check.

Here is an example of a 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}!"); } } } 
+9
source

All Articles