Tuesday, February 9, 2021

Creating country flag emoji from country code - c#

Sometimes you might need to show the country flag as an icon or imoji in your webpage. But we don't need to host images of country flags anywhere which we can create images from the code itself. look at the below code.

 public static string CountryCodeToFlag(this string country)  
     {  
       return string.Concat(country.ToUpper().Select(x => char.ConvertFromUtf32(x + 0x1F1A5)));  
     }  

This is an extension method. so we can use it as below.

 "us".CountryCodeToFlag()  

The result will look like below,

Happy coding...


No comments:

Post a Comment