c# 屏蔽字符串中电话号码

用正则表达式了 Text.RegularExpressions//From:www.uzhanbao.com

//符合XXX-XXXXXXX"、"XXXX-XXXXXXXX"、"XXX-XXXXXXX"、"XXX-XXXXXXXX"、"XXXXXXX"和"XXXXXXXX"。
Regex rx = new Regex(@"(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}",
RegexOptions.Compiled | RegexOptions.IgnoreCase);

//原字符串
string text = "www.subeiwang.com 123-1681111 Good morning 1317878 The end";
//获得匹配项集合
MatchCollection matches = rx.Matches(text);

Console.WriteLine("{0} matches found.", matches.Count);

int count = 0;
foreach (Match match in matches)
{
    string word = match.Groups["word"].Value;

    //删除匹配项
    text = text.Remove(match.Index - count, match.Length);
    count += match.Length;
}
Console.WriteLine(text);

你可能感兴趣的:(C#)