我有一个国家代码的手机号码列表,如“919638095998”号码。我也引用了libphonenumber google example,但首先我们需要传递国家2位数代码,我最初并不知道。那么我怎么知道这个号码来自哪个国家?
我还有一个解决方案,从手机号码手动提取代码,然后与国家/地区代码进行比较。但这是一项长期任务,也会给我带来糟糕的表现。有谁能建议一个合适的解决方案?
答案 0 :(得分:4)
维基百科上有国家/地区代码可用的所有电话号码列表。就在这里
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则检查前四个数字的四位数代码的散列图 如果发现四位数代码返回相应的country.if不回答是美国 否则检查包含2或3位数的散列图中的前两位数。如果找到两个然后返回答案,那么肯定是三位数代码并从散列中返回相应的国家/地区。
制作hashmap会很繁琐,但我认为这是一种有效的解决方案。
答案 1 :(得分:2)
如果不迟,
只是寻找这个我找到了由Google维护的Java和Javascript库;)
查看回购: https://github.com/googlei18n/libphonenumber
还有一个演示。
祝你好运!
答案 2 :(得分:1)
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());
}
答案 3 :(得分: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();
}
}
答案 4 :(得分:0)
虽然使用静态数据结构可能被视为不良做法,但国家/地区代码的变化不大,因此我认为创建一个列表作为树并使用此树解析数字应该是一个易于实现的解决方案。
您还可以将国家/地区代码列表放在外部文件中,以便在不更改应用程序代码的情况下轻松更新。
维基百科有国家代码列表here。
答案 5 :(得分:0)
如果国家/地区代码只有2位数字。将电话号码拆分为2,第一个子字符串有2个数字,第二个子字符串有剩余的10个数字。
答案 6 :(得分:0)
只是从wikipedia进行快速检查,我认为你需要从前面做一个最大字符串搜索算法。在一个似乎没有来的冲突中,你可以查看位数(NSN)来查找国家代码
优化的方法是创建存储桶。从country code list开始。国家代码的最大长度为4,(除非你想进入省份的精确度,在这种情况下它将是7,检查区域6)。然后从4,3,2,1检查是否存在。这将显着缩短搜索算法时间。但是,数据大小会更大。时间空间权衡的经典案例
答案 7 :(得分:0)
如果您查看自己的链接,可以找到CountryCodeToRegionCodeMap class。它包含可能所有国家/地区代码的列表。您可以使用它来创建自己的代码,以确定国家/地区代码的长度为1,2或3.我无法相信这是互联网上唯一可以找到此类列表的地方。
答案 8 :(得分:0)
移动设备中的国家/地区代码某些时间以+(加号)44,0044,44开头,如上所述某些国家/地区可能有可变长度代码。所以最好的解决方案是从现在开始我们知道手机号码的长度是10.一旦你拿走这10个数字,那么你可以相应地解析国家代码。