【问题标题】:Get emoji flag by country code按国家代码获取 emoji 标志
【发布时间】:2017-02-14 19:32:57
【问题描述】:

有一个国家代码列表,我需要为每个国家代码附加表情符号。 有没有办法从中提取 unicode 或找到国家代码的表情符号?

这个 npm 示例与我的目标相似(但使用十六进制作为输入)https://github.com/thekelvinliu/country-code-emoji/blob/master/src/index.js

【问题讨论】:

  • it uses hex as input 是什么意思?这个方法需要一个字符串,throw new TypeError('argument must be a string'); 你有什么问题?为什么你不能只用 Java 重写它,如果这是你的用例,看起来你需要的只是从 0 到第一个 unicode 字符的偏移量。

标签: java android


【解决方案1】:

这段代码 sn-p 对我有用。只需将"US" 替换为您喜欢的任何有效国家/地区代码(基于区域指示符符号字母),它就会创建一个字符串flag,其中包含该国家/地区的国旗表情符号。 (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

【讨论】:

  • 方形emoji国旗怎么做?
  • @NitinKarale 您应该创建一个新问题,而不是使用评论。简短的回答是你无法控制它。
  • 我喜欢这种方法,但我需要 Angular 中的这个功能,而 JavaScript/TypeScript 对字符没有相同的功能; Angular 也可以这样吗?
  • @TorstenN。你必须做另一个问题。 SO 不是论坛。
【解决方案2】:
function getFlags($code){
    $code = strtoupper($code);
    if($code == 'AD') return '??';
    if($code == 'AE') return '??';
    if($code == 'AF') return '??';
    if($code == 'AG') return '??';
    if($code == 'AI') return '??';
    if($code == 'AL') return '??';
    if($code == 'AM') return '??';
    if($code == 'AO') return '??';
    if($code == 'AQ') return '??';
    if($code == 'AR') return '??';
    if($code == 'AS') return '??';
    if($code == 'AT') return '??';
    if($code == 'AU') return '??';
    if($code == 'AW') return '??';
    if($code == 'AX') return '??';
    if($code == 'AZ') return '??';
    if($code == 'BA') return '??';
    if($code == 'BB') return '??';
    if($code == 'BD') return '??';
    if($code == 'BE') return '??';
    if($code == 'BF') return '??';
    if($code == 'BG') return '??';
    if($code == 'BH') return '??';
    if($code == 'BI') return '??';
    if($code == 'BJ') return '??';
    if($code == 'BL') return '??';
    if($code == 'BM') return '??';
    if($code == 'BN') return '??';
    if($code == 'BO') return '??';
    if($code == 'BQ') return '??';
    if($code == 'BR') return '??';
    if($code == 'BS') return '??';
    if($code == 'BT') return '??';
    if($code == 'BV') return '??';
    if($code == 'BW') return '??';
    if($code == 'BY') return '??';
    if($code == 'BZ') return '??';
    if($code == 'CA') return '??';
    if($code == 'CC') return '??';
    if($code == 'CD') return '??';
    if($code == 'CF') return '??';
    if($code == 'CG') return '??';
    if($code == 'CH') return '??';
    if($code == 'CI') return '??';
    if($code == 'CK') return '??';
    if($code == 'CL') return '??';
    if($code == 'CM') return '??';
    if($code == 'CN') return '??';
    if($code == 'CO') return '??';
    if($code == 'CR') return '??';
    if($code == 'CU') return '??';
    if($code == 'CV') return '??';
    if($code == 'CW') return '??';
    if($code == 'CX') return '??';
    if($code == 'CY') return '??';
    if($code == 'CZ') return '??';
    if($code == 'DE') return '??';
    if($code == 'DJ') return '??';
    if($code == 'DK') return '??';
    if($code == 'DM') return '??';
    if($code == 'DO') return '??';
    if($code == 'DZ') return '??';
    if($code == 'EC') return '??';
    if($code == 'EE') return '??';
    if($code == 'EG') return '??';
    if($code == 'EH') return '??';
    if($code == 'ER') return '??';
    if($code == 'ES') return '??';
    if($code == 'ET') return '??';
    if($code == 'FI') return '??';
    if($code == 'FJ') return '??';
    if($code == 'FK') return '??';
    if($code == 'FM') return '??';
    if($code == 'FO') return '??';
    if($code == 'FR') return '??';
    if($code == 'GA') return '??';
    if($code == 'GB') return '??';
    if($code == 'GD') return '??';
    if($code == 'GE') return '??';
    if($code == 'GF') return '??';
    if($code == 'GG') return '??';
    if($code == 'GH') return '??';
    if($code == 'GI') return '??';
    if($code == 'GL') return '??';
    if($code == 'GM') return '??';
    if($code == 'GN') return '??';
    if($code == 'GP') return '??';
    if($code == 'GQ') return '??';
    if($code == 'GR') return '??';
    if($code == 'GS') return '??';
    if($code == 'GT') return '??';
    if($code == 'GU') return '??';
    if($code == 'GW') return '??';
    if($code == 'GY') return '??';
    if($code == 'HK') return '??';
    if($code == 'HM') return '??';
    if($code == 'HN') return '??';
    if($code == 'HR') return '??';
    if($code == 'HT') return '??';
    if($code == 'HU') return '??';
    if($code == 'ID') return '??';
    if($code == 'IE') return '??';
    if($code == 'IL') return '??';
    if($code == 'IM') return '??';
    if($code == 'IN') return '??';
    if($code == 'IO') return '??';
    if($code == 'IQ') return '??';
    if($code == 'IR') return '??';
    if($code == 'IS') return '??';
    if($code == 'IT') return '??';
    if($code == 'JE') return '??';
    if($code == 'JM') return '??';
    if($code == 'JO') return '??';
    if($code == 'JP') return '??';
    if($code == 'KE') return '??';
    if($code == 'KG') return '??';
    if($code == 'KH') return '??';
    if($code == 'KI') return '??';
    if($code == 'KM') return '??';
    if($code == 'KN') return '??';
    if($code == 'KP') return '??';
    if($code == 'KR') return '??';
    if($code == 'KW') return '??';
    if($code == 'KY') return '??';
    if($code == 'KZ') return '??';
    if($code == 'LA') return '??';
    if($code == 'LB') return '??';
    if($code == 'LC') return '??';
    if($code == 'LI') return '??';
    if($code == 'LK') return '??';
    if($code == 'LR') return '??';
    if($code == 'LS') return '??';
    if($code == 'LT') return '??';
    if($code == 'LU') return '??';
    if($code == 'LV') return '??';
    if($code == 'LY') return '??';
    if($code == 'MA') return '??';
    if($code == 'MC') return '??';
    if($code == 'MD') return '??';
    if($code == 'ME') return '??';
    if($code == 'MF') return '??';
    if($code == 'MG') return '??';
    if($code == 'MH') return '??';
    if($code == 'MK') return '??';
    if($code == 'ML') return '??';
    if($code == 'MM') return '??';
    if($code == 'MN') return '??';
    if($code == 'MO') return '??';
    if($code == 'MP') return '??';
    if($code == 'MQ') return '??';
    if($code == 'MR') return '??';
    if($code == 'MS') return '??';
    if($code == 'MT') return '??';
    if($code == 'MU') return '??';
    if($code == 'MV') return '??';
    if($code == 'MW') return '??';
    if($code == 'MX') return '??';
    if($code == 'MY') return '??';
    if($code == 'MZ') return '??';
    if($code == 'NA') return '??';
    if($code == 'NC') return '??';
    if($code == 'NE') return '??';
    if($code == 'NF') return '??';
    if($code == 'NG') return '??';
    if($code == 'NI') return '??';
    if($code == 'NL') return '??';
    if($code == 'NO') return '??';
    if($code == 'NP') return '??';
    if($code == 'NR') return '??';
    if($code == 'NU') return '??';
    if($code == 'NZ') return '??';
    if($code == 'OM') return '??';
    if($code == 'PA') return '??';
    if($code == 'PE') return '??';
    if($code == 'PF') return '??';
    if($code == 'PG') return '??';
    if($code == 'PH') return '??';
    if($code == 'PK') return '??';
    if($code == 'PL') return '??';
    if($code == 'PM') return '??';
    if($code == 'PN') return '??';
    if($code == 'PR') return '??';
    if($code == 'PS') return '??';
    if($code == 'PT') return '??';
    if($code == 'PW') return '??';
    if($code == 'PY') return '??';
    if($code == 'QA') return '??';
    if($code == 'RE') return '??';
    if($code == 'RO') return '??';
    if($code == 'RS') return '??';
    if($code == 'RU') return '??';
    if($code == 'RW') return '??';
    if($code == 'SA') return '??';
    if($code == 'SB') return '??';
    if($code == 'SC') return '??';
    if($code == 'SD') return '??';
    if($code == 'SE') return '??';
    if($code == 'SG') return '??';
    if($code == 'SH') return '??';
    if($code == 'SI') return '??';
    if($code == 'SJ') return '??';
    if($code == 'SK') return '??';
    if($code == 'SL') return '??';
    if($code == 'SM') return '??';
    if($code == 'SN') return '??';
    if($code == 'SO') return '??';
    if($code == 'SR') return '??';
    if($code == 'SS') return '??';
    if($code == 'ST') return '??';
    if($code == 'SV') return '??';
    if($code == 'SX') return '??';
    if($code == 'SY') return '??';
    if($code == 'SZ') return '??';
    if($code == 'TC') return '??';
    if($code == 'TD') return '??';
    if($code == 'TF') return '??';
    if($code == 'TG') return '??';
    if($code == 'TH') return '??';
    if($code == 'TJ') return '??';
    if($code == 'TK') return '??';
    if($code == 'TL') return '??';
    if($code == 'TM') return '??';
    if($code == 'TN') return '??';
    if($code == 'TO') return '??';
    if($code == 'TR') return '??';
    if($code == 'TT') return '??';
    if($code == 'TV') return '??';
    if($code == 'TW') return '??';
    if($code == 'TZ') return '??';
    if($code == 'UA') return '??';
    if($code == 'UG') return '??';
    if($code == 'UM') return '??';
    if($code == 'US') return '??';
    if($code == 'UY') return '??';
    if($code == 'UZ') return '??';
    if($code == 'VA') return '??';
    if($code == 'VC') return '??';
    if($code == 'VE') return '??';
    if($code == 'VG') return '??';
    if($code == 'VI') return '??';
    if($code == 'VN') return '??';
    if($code == 'VU') return '??';
    if($code == 'WF') return '??';
    if($code == 'WS') return '??';
    if($code == 'XK') return '??';
    if($code == 'YE') return '??';
    if($code == 'YT') return '??';
    if($code == 'ZA') return '??';
    if($code == 'ZM') return '??';
    return '?';
}

【讨论】:

  • +1 感谢它在 PHP 和 JavaScript 方面帮助了我,但请记住这是线性搜索,因此如果大量使用它不会提供最佳性能。
  • @Accountantم 在编译语言中不能以这种方式工作。在 C++ 或 C# 中,结构在评估点跳转。
  • @Leandro 即使进行短路评估,它仍然是线性搜索。这比哈希查找效率低:平均而言,O(n) vs O(1)。它与语言是编译还是解释无关。
  • 好的伙计们,你应该了解结构在基础级别是如何工作的
【解决方案3】:

对于 PHP 我总是用这个,用 hex2bin 计算字符索引:


function parseCountryCodeToFlag(string $code): string
{
    $exceptions = [
        'en' => 'gb',
        'uk' => 'gb',
    ];

    $code = str_replace(array_keys($exceptions), array_values($exceptions), $code);

    $emoji = [];
    foreach(str_split($code) as $c) {
        if(($o = ord($c)) > 64 && $o % 32 < 27) {
            $emoji[] = hex2bin("f09f87" . dechex($o % 32 + 165));
            continue;
        }
        
        $emoji[] = $c;
    }

    return join($emoji);
}

【讨论】:

    【解决方案4】:

    如果有人在寻找 Unity/C# 版本...

    public string GetCountryFlagEmojiFromCode(string countryCode) {
    
        int flagOffset = 0x1F1E6;
        int asciiOffset = 0x41;
    
        int firstChar = char.ConvertToUtf32(countryCode, 0) - asciiOffset + flagOffset;
        int secondChar = char.ConvertToUtf32(countryCode, 1) - asciiOffset + flagOffset;
            
        String flag = char.ConvertFromUtf32(firstChar) + char.ConvertFromUtf32(secondChar);
        return flag;
    
    }
    

    【讨论】:

      【解决方案5】:
              function getFlagEmoji(countryCode)
              {
                  const codePoints = 
                      countryCode
                          .toUpperCase()
                          .split('')
                          .map( char => 127397 + char.charCodeAt() );
      
                  return String.fromCodePoint(...codePoints);
              }
      
             console.log(getFlagEmoji( 'PK' ));
      

      【讨论】:

        猜你喜欢
        • 2015-08-10
        • 2014-12-01
        • 2017-04-09
        • 1970-01-01
        • 1970-01-01
        • 2016-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多