extract the country code from mobile number

若如初见. 提交于 2019-12-21 02:35:24

问题


I have a list of mobile numbers with country code like "919638095998"number. I have referred the libphonenumber google example also but in that first we need to pass the country 2 digit code, which I don't know initially. So how can I know that the number is from which country?

I have also got one solution as manually extract the code one by one digit from mobile number and then compare with country code. But it is a long task and also it will give me bad performance. Can anyone suggest a proper solution?


回答1:


There is a list of all the phone numbers available for country codes on Wikipedia. It is here

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

I would create two hashmaps. One with all the four digit codes and one containing both the two and three digit 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. 
     }

In summary. If first digit is 7 Russian. If it is 1 check in the hashmap of four digit codes for the first four numbers if found a four digit code return the corresponding country.if not answer is USA otherwise check first two digits in the hashmap containing the 2 or 3 digits. If found two then return the answer other it is certainly a three digit code and return the corresponding country from the hash.

It will be tedious to make the hashmap but I think an efficient solution.




回答2:


libphonenumber does that for you. All you have to do is add the + sign before your phone number and omit the country code when parsing the phone number.

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());
}



回答3:


if it's not late

Just looking for this I found a Java and Javascript library maintained by Google ;)

See the repo: https://github.com/googlei18n/libphonenumber

Also there is a demo.

Good Luck!




回答4:


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();
        }

    }



回答5:


While using static data structures may be considered bad practice, country codes don't change much, so I think creating a list of them as a tree and parsing the number with this tree should be an easy to implement solution.

You could also put the list of country codes in an external file so it can be easily updated without changing the application code.

Wikipedia has the list of country codes here.




回答6:


If the country code is only 2 digit no. split the phone no into 2 with first substring having 2 digits and second one having the remaining 10 digit.




回答7:


If you take a look at your own link you can find the CountryCodeToRegionCodeMap class. It contains a list of probably all country codes. You could use that to create your own code to determine of the country code has a length of 1, 2 or 3. I can't believe this is the only place on the internet where such a list can be found.




回答8:


Just doing a quick check from wikipedia, I figured that you need to do a max string search algo from the front. On a conflict, which does not seem to come, but incase, you can check the number of digits (NSN) to find the country code

An optimized way would be to create buckets. As from country code list. The maximum length of country code is 4, (unless you want to go into the precision of provinces in which case it will be 7, check zone 6). and then check for the existence from 4, 3, 2, 1. This will significantly reduce your search algorithm time. But yes the data size will be bigger. A classic case of time space tradeoff




回答9:


Country code in mobile some time start with like +(plus)44, 0044, 44 and as said above some country may have variable length code. So best possible solution would be start from back as of now we know length of mobile number is 10. once you take those 10 digits, Then you can parse country code accordingly.



来源:https://stackoverflow.com/questions/12259347/extract-the-country-code-from-mobile-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!