我想检测并删除字符串中的重复项。
我的代码在这里:
string Tests = "Hi,World,Me,Hi,You";
string[] Tests_Array = Tests.Split(',');
Regex FindDup = new Regex(@"(.+)\1", RegexOptions.IgnoreCase);
string t2 = "";
foreach (string test in Tests_Array)
{
MatchCollection allMatches = FindDup.Matches(test);
foreach (Match item in allMatches)
{
t2 = FindDup.Replace(test, string.Empty);
textBox1.Text += string.Format(@"Final: ""{0}""", t2) + "\n";
}
}
但它不起作用。
不知道哪里出了问题?
感谢您的帮助。
最佳答案
你可以使用 LINQ
string Tests = "Hi,World,Me,Hi,You";
string[] Tests_Array = Tests.Split(',');
string result = String.Join(",", Tests_Array.Distinct());
关于c# - 在 C# 中检测字符串中的重复单词时遇到问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23827221/