在C#中从字符串中删除重复的单词

3

这是我的代码:

class Program
    {
        static void Main(string[] args)
        {
            string sentence = string.Empty;
            sentence = Console.ReadLine();
            string[] sent = sentence.Split(' ');
            //to be sorted alphabetically
            var x =
                from k in sent
                orderby k
                select k;

            foreach (string s in x)
            {
                    Console.WriteLine(s.ToLower());
            }

            Console.ReadLine();
        }
    }

有没有方法可以查找和删除重复的单词,还是我需要自己编写一个方法?


可能是在C#中从List<T>中删除重复项的重复问题。 - 123 456 789 0
5个回答

11
你可以使用 Linq 的 Distinct 扩展方法: Distinct
var sent = sentence.Split(' ').Distinct();

当比较字符串时,您也可以使用这个函数来忽略它们的大小写—例如,"WORD""word"会被视为重复:

var sent = sentence.Split(' ').Distinct(StringComparer.CurrentCultureIgnoreCase);

8

使用System.Linq的Distinct方法:

foreach (string s in x.Distinct())

4

Use Distinct:

foreach (string s in x.Distinct())
{
        Console.WriteLine(s.ToLower());
}

0

这个应该能满足你的所有要求:

class Program
{
    static void Main(string[] args)
    {
        string sentence = string.Empty;
        sentence = Console.ReadLine();

        var sent = sentence
            .Split(' ')
            .Distinct()
            .OrderBy(x => x);

        foreach (string s in sent)
        {
            Console.WriteLine(s.ToLower());
        }

        Console.ReadLine();
    }
}

希望有所帮助!

0

使用for循环从字符串中简单删除重复的单词。 尝试一下,它可以正常工作。

string SetenceString = "red white black white green yellow red red black white";
        string[] data = SetenceString.Split(' ');
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < data.Length; i++)
        {
            string temp = " ";
            if (i == 0)
            {
                temp = data[i].ToString();
                sb = sb.Append(temp + " ");

            }
            else
            {
                for (int j = 1; j < data.Length; j++)
                {
                    string temp2 = data[j].ToString();
                    string strnew = sb.ToString();
                    string[] Kdata = strnew.Split(' ');
                    bool isnoduplicate = false;
                    for (int k = 0; k < Kdata.Length; k++)
                    {
                        string temp3 = Kdata[k].ToString();

                      if (temp3 != "")
                        {
                            if (temp2 == temp3)
                            {
                                isnoduplicate = false;
                                break;
                            }
                            else
                            {
                                isnoduplicate = true;
                            }
                        }
                    }
                    if (isnoduplicate)
                    {
                        sb = sb.Append(temp2 + " ");
                    }

                }
            }
        }
        Console.WriteLine(sb);
        Console.ReadKey();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接