今天遇到了一個問題,給一個手機號碼,怎樣判斷它是移動的還是聯通的。我自己查了一些資料,諮詢了一些朋友。不知道是否全面,想和大家研究一下。當然我指的是業務邏輯是否正確,並不是程序本身。用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"}) ;
* 判斷號碼是聯通,移動,電信中的哪個,
* 在使用本方法前,請先驗證號碼的合法性 規則:前三位爲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))) ;
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" ; //其他爲電信
}
return "1" ;//聯通
if ( bolChinaMobile1 || bolChinaMobile2 )
return "2" ; //移動
return "3" ; //其他爲電信
}