C#实现字符串去重
2021-06-22 04:06
标签:static int return ace 框架 使用 regex color read 方法一 注:需要.net 3.5框架的支持 方法二: 方法三:使用正则 C#实现字符串去重 标签:static int return ace 框架 使用 regex color read 原文地址:https://www.cnblogs.com/xiaoyongjun/p/10218504.htmlstring 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 )
{
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 )
{
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));