remove duplicate words from string in C#

前端 未结 4 559
猫巷女王i
猫巷女王i 2020-12-19 20:05

here is my code:

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


        
4条回答
  •  旧巷少年郎
    2020-12-19 20:32

    This should do everything you're asking:

    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();
        }
    }
    

    Hope it helps!

提交回复
热议问题