Java 从手机号码中提取国家/地区代码

Java 从手机号码中提取国家/地区代码,java,android,contacts,Java,Android,Contacts,我有一个国家代码为“919638095998”的手机号码列表。我也提到了这个问题,但在第一个问题中,我们需要传递国家2位代码,我一开始不知道。那么我怎么知道这个号码来自哪个国家 我也有一个解决方案,手动从手机号码中逐个提取代码,然后与国家代码进行比较。但这是一项很长的任务,而且会给我带来糟糕的表现。有人能推荐一个合适的解决方案吗?虽然使用静态数据结构可能被认为是不好的做法,但国家代码变化不大,所以我认为创建一个树型列表并用这个树解析数字应该是一个容易实现的解决方案 您还可以将国家/地区代码列表放

我有一个国家代码为“919638095998”的手机号码列表。我也提到了这个问题,但在第一个问题中,我们需要传递国家2位代码,我一开始不知道。那么我怎么知道这个号码来自哪个国家


我也有一个解决方案,手动从手机号码中逐个提取代码,然后与国家代码进行比较。但这是一项很长的任务,而且会给我带来糟糕的表现。有人能推荐一个合适的解决方案吗?

虽然使用静态数据结构可能被认为是不好的做法,但国家代码变化不大,所以我认为创建一个树型列表并用这个树解析数字应该是一个容易实现的解决方案

您还可以将国家/地区代码列表放入外部文件中,以便在不更改应用程序代码的情况下轻松更新


Wikipedia有国家代码列表。

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

只需快速检查一下,我想你需要从前面执行最大字符串搜索算法。在冲突中,似乎不会发生冲突,但在这种情况下,您可以检查位数(NSN)以查找国家代码


一种优化的方法是创建bucket。从那时起。国家/地区代码的最大长度为4(除非您想输入省份的精度,在这种情况下,它将为7,请选中区域6)。然后从4,3,2,1检查是否存在。这将大大减少搜索算法的时间。但是是的,数据量会更大。时空权衡的经典案例

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

维基百科上有一个关于国家代码的所有电话号码的列表。在这儿

我将创建两个hashmap。一个包含所有四位代码,另一个包含两位和三位代码

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. 
     }
Hashmap-fourdigitcodes;
//检查第一位数字是否为7
如果(第一位数==7)
//国家绝对是俄罗斯
其他的
如果国家代码以1开头。
检查hashmap中的前四位是否包含四位代码,然后返回值
若并没有找到值,那个么答案肯定是usa。
否则
字符串country=“”;
HashMap ccode=新的HashMap();
ccode.put(“353”、“爱尔兰”)//列出所有国家/地区代码
第49号(“德国”);
字符串值=ccode.get(“49”)//查看前两位是否有值
if(值!=null){
country=value;//如果我为前两位数字找到了一个国家,我们就完成了,因为由两位数字组成的国家代码的前两位数字不能包含在带有3的国家代码中
}否则{
国家/地区当然是3位数字。输入前3位数字作为hashmap的键返回前三位数字。
}
总之。如果第一个数字是7俄文。 如果是1,则检查前四个数字的四位代码哈希图 如果找到四位数代码,返回相应的国家/地区。如果没有,回答是美国 否则,请检查包含2或3位数字的hashmap中的前两位数字。如果找到两个,则返回另一个答案,它肯定是一个三位数代码,并从散列中返回相应的国家


制作hashmap会很枯燥,但我认为这是一个有效的解决方案。

手机中的国家代码有时以like+(plus)440044,44开头,如上所述,一些国家可能有可变长度的代码。所以最好的解决方案是从头开始,现在我们知道手机号码的长度是10。一旦取下这10个数字,就可以相应地解析国家代码

如果不晚的话

我发现了一个由Google维护的Java和Javascript库;)

见回购协议:

还有一个演示

祝你好运

这是为你做的。您只需在电话号码前添加
+
符号,并在解析电话号码时省略国家代码

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());
}
下面的代码将从任何给定的电话号码中提取国家/地区代码,逻辑是基于。https://en.wikipedia.org/wiki/List_of_country_calling_codes
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
/**
*@author swapna*此类将从传入的
*电话号码,格式基于
*数据来自https://en.wikipedia.org/wiki/List_of_country_calling_codes
*
*/
公共枚举PhoneNumberCtryCode{
实例;
私有列表forCtryCodePrefix1=新的ArrayList();
@抑制警告(“串行”)
List forCtryCodePrefix2=新的ArrayList(){
{
加(0);
增加(7);
增加(8);
}
};
@抑制警告(“串行”)
私有列表forCtryCodePrefix3=新ArrayList(){
{
加(0);
增加(1);
增加(2);
增加(3);
增加(4);
增加(6);
增加(9);
}
};
@抑制警告(“串行”)
私有列表forCtryCodePrefix4=新ArrayList(){
{
加(0);
增加(1);
增加(3);
增加(4);
增加(5);
增加(6);
增加(7);
增加(8);
增加(9);
}
};
@抑制警告(“串行”)
私有列表forCtryCodePrefix5=新ArrayList(){
{
增加(1);
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();
        }

    }