电话号码的判断--使用正则表达式的示例
code copy from http://blog.csdn.net/kwklover/archive/2005/01/12/250326.aspx
thanks to kwklover
怕忘了所以copy
/// <summary>
/// 判断号码是联通,移动,电信中的哪个,在使用本方法前,请先验证号码的合法性
/// 规则:前三位为130-133 联通 ;前三位为135-139或前四位为1340-1348 移动; 其它的应该为电信
/// </summary>
/// <param name="mobile">要判断的号码</param>
/// <returns>返回相应类型:1代表联通;2代表移动;3代表电信</returns>
public static int GetMobileType(string mobile)
{
string[] chinaUnicom = new string[] {"130","131","132","133"} ;
string[] chinaMobile1 = new string[] {"135","136","137","138","139"} ;
string[] chinaMobile2 = new string[] {"1340","1341","1342","1343","1344","1345","1346","1347","1348"} ;
bool bolChinaUnicom = (Array.IndexOf(chinaUnicom,mobile.Substring(0,3)) >= 0) ;
bool bolChinaMobile1 = (Array.IndexOf(chinaMobile1,mobile.Substring(0,3)) >=0) ;
bool bolChinaMobile2 = (Array.IndexOf(chinaMobile2,mobile.Substring(0,4)) >=0) ;
if (bolChinaUnicom)
return 1 ;//联通
if ( bolChinaMobile1 || bolChinaMobile2 )
return 2 ; //移动
return 3 ; //其他为电信
}
注:有朋友建议,第二方法用正则表达式实现更好,确实不错,下面把第二方法的新实现贴上:
/// <summary>
/// 判断号码是联通,移动,电信中的哪个,在使用本方法前,请先验证号码的合法性
/// 规则:前三位为130-133 联通 ;前三位为135-139或前四位为1340-1348 移动; 其它的应该为电信
/// </summary>
/// <param name="mobile">要判断的号码</param>
/// <returns>返回相应类型:1代表联通;2代表移动;3代表电信</returns>
public static int GetMobileType(string mobile)
{
if (IsChinaUnicomNumber(mobile))
return 1 ;
if (IsChinaMobileNumber(mobile))
return 2 ;
return 3 ;
}
//是否是联通的号码 测试通过
private static bool IsChinaUnicomNumber(string mobile)
{
string sPattern = "^(130|131|132|133)[0-9]{8}";
bool isChinaUnicom = Regex.IsMatch(mobile,sPattern) ;
return isChinaUnicom ;
}
//是否是移动的号码 测试通过
private static bool IsChinaMobileNumber(string mobile)
{
string sPattern = "^(135|136|137|138|139|1340|1341|1342|1343|1344|1345|1346|1347|1348)[1-9]{7,8}" ;
return Regex.IsMatch(mobile,sPattern) ;
}
版权声明:CSDN是本Blog托管服务提供商。如本文牵涉版权问题,CSDN不承担相关责任,请版权拥有者直接与文章作者联系解决。
thanks to kwklover
怕忘了所以copy
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
注:有朋友建议,第二方法用正则表达式实现更好,确实不错,下面把第二方法的新实现贴上:
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
版权声明:CSDN是本Blog托管服务提供商。如本文牵涉版权问题,CSDN不承担相关责任,请版权拥有者直接与文章作者联系解决。