从手机号码中提取国家代码

问题描述 投票:0回答:10

我有一个带有国家代码的手机号码列表,例如“919638095998”号码。我还提到了 libphonenumber google 示例,但首先我们需要传递国家/地区 2 位数代码,我最初不知道。那么我怎么知道这个号码来自哪个国家呢?

我还得到了一种解决方案,即手动从手机号码中一位一位地提取代码,然后与国家代码进行比较。但这是一项漫长的任务,而且会给我带来不好的表现。谁能提出一个合适的解决方案?

java android contacts
10个回答
13
投票

libphonenumber 可以为你做到这一点。您所要做的就是在电话号码前添加

+
符号,并在解析电话号码时省略国家/地区代码。

String numberStr = "+919638095998";
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
    Phonenumber.PhoneNumber numberProto = phoneUtil.parse(numberStr, "");

    System.out.println("Country code: " + numberProto.getCountryCode());
    //This prints "Country code: 91"
} catch (NumberParseException e) {
    System.err.println("NumberParseException was thrown: " + e.toString());
}

9
投票

维基百科上有一个可用于国家/地区代码的所有电话号码的列表。就在这里

http://en.wikipedia.org/wiki/List_of_country_calling_codes

我会创建两个哈希图。一种包含所有四位数代码,另一种包含两位和三位数代码。

Hashmap fourdigitcodes;

//check if first digit is 7
if(firstdigit==7)
  //country definitely russia
else
   if country code begins with 1.
     Check the first four digits are in the hashmap with four digit codes and return the value 
     if no value is found then answer is certainly usa.
otherwise
     String country="";
     HashMap<String,String> ccode = new HashMap<String,String>();
     ccode.put("353","Ireland");//List all country codes
     ccode.put("49","Germany");
     String value = ccode.get("49");//See if I have a value for the first 2 digits
     if(value != null){
      country = value; //If I found a country for the first two digits we are done as the              first two digits of the country code consisting of two digits cannot be in a country code with 3
     }else{
      the country is certainly 3 digit. return the first three digits by entering the first 3 digits as the key of the hashmap countaining the first three digits. 
     }

总而言之。如果第一个数字是 7 俄语。 如果是 1,请检查前四个数字的四位代码的哈希映射 如果找到四位数代码,则返回相应的国家/地区。如果没有,则返回美国 否则检查包含 2 或 3 位数字的哈希图中的前两位数字。如果找到两个则返回答案,其他则肯定是三位数代码并从哈希中返回相应的国家/地区。

制作哈希图会很乏味,但我认为这是一个有效的解决方案。


2
投票
The below code will extract the country code from any given phone number, the logic is implemented based on. https://en.wikipedia.org/wiki/List_of_country_calling_codes
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

    /**
     * @author swapna * This class will fetch the country code from the incoming
     *         phone number with format <+><CTRY CODE><Phone Number> based on the
     *         data from https://en.wikipedia.org/wiki/List_of_country_calling_codes
     *
     */
    public enum PhoneNumberNCtryCode {
        Instance;

        private List<Integer> forCtryCodePrefix1 = new ArrayList<Integer>();
        @SuppressWarnings("serial")
        List<Integer> forCtryCodePrefix2 = new ArrayList<Integer>() {
            {
                add(0);
                add(7);
                add(8);

            }
        };
        @SuppressWarnings("serial")
        private List<Integer> forCtryCodePrefix3 = new ArrayList<Integer>() {
            {
                add(0);
                add(1);
                add(2);
                add(3);
                add(4);
                add(6);
                add(9);
            }
        };
        @SuppressWarnings("serial")
        private List<Integer> forCtryCodePrefix4 = new ArrayList<Integer>() {
            {
                add(0);
                add(1);
                add(3);
                add(4);
                add(5);
                add(6);
                add(7);
                add(8);
                add(9);
            }
        };
        @SuppressWarnings("serial")
        private List<Integer> forCtryCodePrefix5 = new ArrayList<Integer>() {
            {
                add(1);
                add(2);
                add(3);
                add(4);
                add(5);
                add(6);
                add(7);
                add(8);
            }
        };
        @SuppressWarnings("serial")
        private List<Integer> forCtryCodePrefix6 = new ArrayList<Integer>() {
            {
                add(0);
                add(1);
                add(3);
                add(4);
                add(5);
                add(6);

            }
        };
        private List<Integer> forCtryCodePrefix7 = new ArrayList<Integer>();
        @SuppressWarnings("serial")
        private List<Integer> forCtryCodePrefix8 = new ArrayList<Integer>() {
            {
                add(1);
                add(3);
                add(4);
                add(6);
                add(9);

            }
        };
        @SuppressWarnings("serial")
        private List<Integer> forCtryCodePrefix9 = new ArrayList<Integer>() {
            {
                add(1);
                add(2);
                add(3);
                add(4);
                add(5);
                add(8);
            }
        };
        @SuppressWarnings("serial")
        private Map<Integer, List<Integer>> countryCodeMap = new HashMap<Integer, List<Integer>>() {
            {
                put(1, forCtryCodePrefix1);
                put(2, forCtryCodePrefix2);
                put(3, forCtryCodePrefix3);
                put(4, forCtryCodePrefix4);
                put(5, forCtryCodePrefix5);
                put(6, forCtryCodePrefix6);
                put(7, forCtryCodePrefix7);
                put(8, forCtryCodePrefix8);
                put(9, forCtryCodePrefix9);

            }
        };

        /**
         * This method parses the phone number using the prefix Ctry Map
         * 
         * @param phoneNumber
         * @return
         */
        public int getCtryCode(String phoneNumber) {
            String ctryCodeAtIndex1 = phoneNumber.substring(1, 2);
            Integer ctryCode = 0;
            String ctryCodeStr = "0";

            List<Integer> ctryCodeList =        countryCodeMap.get(Integer.valueOf(ctryCodeAtIndex1));
            if (ctryCodeList.isEmpty()) {
                ctryCode = Integer.valueOf(ctryCodeAtIndex1);
                return ctryCode.intValue();
            }
            String ctryCodeAtIndex2 = phoneNumber.substring(2, 3);
            for (Integer ctryCodePrefix : ctryCodeList) {
                if (Integer.valueOf(ctryCodeAtIndex2) == ctryCodePrefix) {
                    ctryCodeStr = phoneNumber.substring(1, 3);
                    ctryCode = Integer.valueOf(ctryCodeStr);
                    return ctryCode.intValue();

                }
            }
            ctryCodeStr = phoneNumber.substring(1, 4);
            ctryCode = Integer.valueOf(ctryCodeStr);
            return ctryCode.intValue();
        }

    }

1
投票

如果不晚的话

只是为了寻找这个,我发现了一个由 Google 维护的 Java 和 Javascript 库;)

查看回购协议: https://github.com/googlei18n/libphonenumber

还有一个演示。

祝你好运!


0
投票

虽然使用静态数据结构可能被认为是不好的做法,但国家/地区代码不会发生太大变化,因此我认为将它们的列表创建为树并用该树解析数字应该是一个易于实现的解决方案。

您还可以将国家/地区代码列表放入外部文件中,以便可以轻松更新,而无需更改应用程序代码。

维基百科有国家代码列表这里


0
投票

如果国家代码只有 2 位数字。将电话号码分成 2 个,第一个子字符串包含 2 位数字,第二个子字符串包含剩余的 10 位数字。


0
投票

如果您查看自己的链接,您可以找到 CountryCodeToRegionCodeMap 类。它包含可能所有国家/地区代码的列表。您可以使用它来创建自己的代码来确定国家/地区代码的长度为 1、2 或 3。我不敢相信这是互联网上唯一可以找到此类列表的地方。


0
投票

只是从wikipedia进行了快速检查,我认为您需要从前面进行最大字符串搜索算法。发生冲突时,似乎不会发生,但以防万一,您可以检查位数(NSN)来查找国家/地区代码

一种优化的方法是创建存储桶。从国家代码列表开始。国家代码的最大长度为 4,(除非您想进入省份的精度,在这种情况下它将是 7,请检查区域 6)。然后检查 4, 3, 2, 1 是否存在。这将显着减少您的搜索算法时间。但是,数据量确实会更大。时空权衡的经典案例


0
投票

手机中的国家/地区代码有时以 +(plus)44、0044、44 等开头,如上所述,某些国家/地区可能有可变长度代码。因此,最好的解决方案是从后面开始,目前我们知道手机号码的长度是 10。一旦你获取了这 10 位数字,那么你就可以相应地解析国家/地区代码。


0
投票

您可以使用 beautiful soup 来抓取此 URL 中的数据(https://www.iban.com/dialing-codes),然后相应地加入 用 python 做是最好的

© www.soinside.com 2019 - 2024. All rights reserved.