remove duplicate words from string in C#

前端 未结 4 557
猫巷女王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:47

    You could use Linq's Distinct extension method:

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

    You can also use this to ignore the case of strings when comparing them—e.g. "WORD" and "word" would be considered duplicates:

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

提交回复
热议问题