如果在一个TEXTBOX里输入一大串号码,中间用,或者其他符号隔开,那就要一些判断把这个字符串解析一下,去掉不符合的号码,去掉重复的号码,得到移动号码,得到联通号码。
最近的项目碰到类似的情况,所以总结了一些东西放在一个类里,如果下次项目再碰到的话,就可以偷懒了,直接调用
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
//using System.Text.RegularExpressions;
namespace Common
{
public class Mobile
{
//过滤掉非联通移动的字符串
public static string IsMobileNums(string mobile)
{
string regular=@"^((\(\d{3}\))|(\d{3}\-))?13[0-9]\d{8}|15[89]\d{8}";
string mobile_new=string.Empty;
string[] p = mobile.Split(',');
foreach (string pp in p)
{
if (System.Text.RegularExpressions.Regex.IsMatch(pp, regular))
{
if (!mobile_new.Equals(string.Empty))
{
mobile_new += "," + pp;
}
else
{
mobile_new = pp;
}
}
}
return mobile_new;
}
//过滤掉字符串中重复的手机号码
public static string IsMobileRepeat(string mobile)
{
ArrayList al = new ArrayList();
string[] p = mobile.Split(',');
for (int i = 0; i < p.Length; i++)
{
if (al.Contains(p[i]) == false)
{
al.Add(p[i]);
}
}
string mobile_new = string.Empty;
//mobile_new=string.Join(",", (string[])al.ToArray(typeof(string)));
string[] pp = (string[])al.ToArray(typeof(string));
for (int i = 0; i < pp.Length; i++)
{
if (!mobile_new.Equals(string.Empty))
{
mobile_new += "," + pp[i].ToString();
}
else
{
mobile_new = pp[i].ToString();
}
}
return mobile_new;
}
//提取字符串中的移动手机号码
public static string IsMobile_YiDong(string mobile)
{
string regular = @"^((\(\d{3}\))|(\d{3}\-))?13[456789]\d{8}|15[89]\d{8}";
string mobile_new = string.Empty;
string[] p = mobile.Split(',');
foreach (string pp in p)
{
if (System.Text.RegularExpressions.Regex.IsMatch(pp, regular))
{
if (!mobile_new.Equals(string.Empty))
{
mobile_new += "," + pp;
}
else
{
mobile_new = pp;
}
}
}
return mobile_new;
}
//提取字符串中的联通号码
public static string IsMobile_LianTong(string mobile)
{
string regular = @"^1(3[0-2]|5[56]|8[56])\d{8}$";
string mobile_new = string.Empty;
string[] p = mobile.Split(',');
foreach (string pp in p)
{
if (System.Text.RegularExpressions.Regex.IsMatch(pp, regular))
{
if (!mobile_new.Equals(string.Empty))
{
mobile_new += "," + pp;
}
else
{
mobile_new = pp;
}
}
}
return mobile_new;
}
}
}
例子下载:主要是为了以后项目服务

浙公网安备 33010602011771号