判断手机号码归属于哪个运营商

2020-07-01 18:10:15 浏览数 (1)

很多时候需要手机号进行注册,并且需要对手机号码归属于那个运营商做一下判断。先来说一下思路:首先,需要获取到用户输入的手机号码,然后将目前三大运营商运营的手机号码前缀分别组成list数组,然后截取用户手机号码前缀,看归属于哪个list,这样就用到了上一篇文章说的list.contains()方法了。下面来看一下例子

代码语言:javascript复制
 public 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",  
	            "145","155", "156", "186", "185" });  
	    List chinaMobile1 = Arrays.asList(new String[] { "135", "136", "137",  
	            "138", "139", "147","150", "151", "152", "157", "158", "159", "182","187",  
	            "188" });  
	    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"; //其他为电信    
	  
	}

0 人点赞