C# 程序用于从给定句子中删除所有重复单词
设置一个包含重复单词的字符串。
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
广告