Java – get emoticon flags by country code

There is a list of country codes. I need to attach emoticons to each code

This NPM example looks similar to my target (but uses hexadecimal as input) https://github.com/thekelvinliu/country-code-emoji/blob/master/src/index.js

Solution

This code snippet is useful to me Simply replace "us" with any valid country code you like (based on the regional indicator symbol), and it will create a string flag containing the symbol emoticon of the country ( Reference)

int flagOffset = 0x1F1E6;
int asciiOffset = 0x41;

String country = "US";

int firstChar = Character.codePointAt(country,0) - asciiOffset + flagOffset;
int secondChar = Character.codePointAt(country,1) - asciiOffset + flagOffset;

String flag = new String(Character.tochars(firstChar))
            + new String(Character.tochars(secondChar));

This answer helped

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>