在字符串数组中查找第一个字符串元素中的重复项

问题描述:

I have an arry of strings:

string[] originText =  { "Lorem ipsum dolor.",  "Quisque at massa non erat.",  "Donec auctor blandit nibh.",  "Donec tincidunt." };


Is there any way to:

1. Get  only the  first string elements and  cast them into a separate  string[] firstElem { "Lorem", "Quisque", "Donec", "Donec"}.
   
2. Compare them  and cast the result into  string[] duplicates (or dictionary) so :
 
Donec - 2 times
Lorem -  1 time
Quisque - 1 time  


Thank you 





我尝试过:



使用



What I have tried:

Using

originText = originText.Where((item, index) => index == 0).ToArray();



我只能获得整个第一个字符串,但不能获得每个字符串的第一个元素。

我太新了C#。


I can get only the whole first string, but not the first element of each string.
I am too new to C#.

试试



try

string[] originText = { "Lorem ipsum dolor.", "Quisque at massa non erat.", "Donec auctor blandit nibh.", "Donec tincidunt." };
          var temp = originText.Select(k => k.Split(' ')[0]).GroupBy(k => k);
          Dictionary<string, int> dictCount = new Dictionary<string, int>();
          foreach (var item in temp)
              dictCount.Add(item.Key, item.Count());