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
广告