C# 程序从给定的句子中删除所有重复的单词
csharpprogrammingserver side programming更新于 2025/9/18 20:12:00
设置一个包含重复单词的字符串。
string str = "One Two Three One";
上面,您可以看到单词"One"出现了两次。
要删除重复的单词,您可以尝试在 C# 中运行以下代码 −
示例
using System;
using System.Linq;
public class Program {
public static void Main() {
string str = "One Two Three One";
string[] arr = str.Split(' ');
Console.WriteLine(str);
var a =
from k in arr
orderby k
select k;
Console.WriteLine("After removing duplicate words...");
foreach(string res in a.Distinct()) {
Console.Write(" " + res.ToLower());
}
Console.ReadLine();
}
}
输出
One Two Three One After removing duplicate words... one three two

