今天遇到了一个问题,给一个手机号码,怎样判断它是移动的还是联通的。我自己查了一些资料,咨询了一些朋友。不知道是否全面,想和大家研究一下。当然我指的是业务逻辑是否正确,并不是程序本身。用java实现的:
/**
* 判断号码是联通,移动,电信中的哪个,
* 在使用本方法前,请先验证号码的合法性 规则:前三位为130-133 联通 ;前三位为135-139或前四位为1340-1348 移动; 其它的应该为电信
* @param mobile要判断的号码
* @return 返回相应类型:1代表联通;2代表移动;3代表电信
*/
public static String getMobileType(String mobile) {
if(mobile.startsWith("0") || mobile.startsWith("+860")){
mobile = mobile.substring(mobile.indexOf("0") + 1, mobile.length());
}
List chinaUnicom = Arrays.asList(new String[] {"130","131","132","133"}) ;
List chinaMobile1 = Arrays.asList(new String[] {"135","136","137","138","139","158","159"}) ;
List chinaMobile2 = Arrays.asList(new String[] {"1340","1341","1342","1343","1344","1345","1346","1347","1348"}) ;
boolean bolChinaUnicom = (chinaUnicom.contains(mobile.substring(0,3))) ;
boolean bolChinaMobile1 = (chinaMobile1.contains(mobile.substring(0,3))) ;
boolean bolChinaMobile2 = (chinaMobile2.contains(mobile.substring(0,4))) ;
if (bolChinaUnicom)
return "1" ;//联通
if ( bolChinaMobile1 || bolChinaMobile2 )
return "2" ; //移动
return "3" ; //其他为电信
}