C#实现字符串去重
方法一 注:需要.net 3.5框架的支持
string s = "101,102,103,104,105,101,102,103,104,105,106,107,101,108";
s = string.Join(",", s.Split(',').Distinct().ToArray());
方法二
class Program
{
static void Main(string[] args)
{
string result="";
string str = "101,102,103,104,105,101,102,103,104,105,106,107,101,108";
ArrayList list = array(str);
for (int i = 0; i < list.Count;i++)
{
if (i == list.Count - 1)
{
result += list[i];
}
else
{
result += list[i] + ",";
}
}
Console.WriteLine(result);
Console.ReadKey();
}
static ArrayList array(string str)
{
ArrayList aimArr = new ArrayList();
ArrayList strArr = new ArrayList();
string [] strs=str.Split(',');
foreach (string s in strs)
{
strArr.Add(s);
}
for (int i = 0; i < strs.Length; i++)
{
if (!aimArr.Contains(strs[i]))
{
aimArr.Add(strs[i]);
}
}
return aimArr;
}
}
//101,102,103,104,105,106,107,108
方法三:使用正则
string input = "101,102,103,104,105,101,102,103,104,105,106,107,101,108";
input = Regex.Replace(input + ",", @"(?:([^,]+,))(?=.*?\1)", "");
Console.WriteLine(input.Substring(0,input.Length-1));
public static void Main()
{
string pattern = @"(^189\d{8}$)|(^13\d{9}$)|(^15\d{9}$)";
Console.WriteLine("输入号码");
string temp = Console.ReadLine();
if (System.Text.RegularExpressions.Regex.IsMatch(temp, pattern)) //正则表达式匹配
Console.WriteLine("手机号输入正确!");
else
Console.WriteLine("手机号输入错误!");
}
我也不知道手机的开头到底有多少种···这个应该可以验证大部分了
比如189开头的,130等,159之类
本人博客的文章大部分来自网络转载,因为时间的关系,没有写明转载出处和作者。所以在些郑重的说明:文章只限交流,版权归作者。谢谢