java提取姓名与手机号码_java – 从手机号码中提取国家/地区代码

本文介绍如何使用Java通过哈希映射来高效地从电话号码中提取国家/地区代码。首先,创建两个哈希图,分别存储四位数和两位、三位数的代码。通过检查首位数字,如7对应俄罗斯,1可能对应美国。如果首位是7,直接确定为俄罗斯;若首位是1,查找四位数代码;否则,查找两位或三位数代码。若找到对应国家,则返回国家名称,否则根据代码长度判断是两位还是三位数的国家/地区代码。
摘要由CSDN通过智能技术生成

维基百科上有国家代码可用的所有电话号码列表.是这里

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

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 ccode = new HashMap();

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会很繁琐,但我认为这是一种有效的解决方案.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值