在字符串数组中查找第一个字符串元素中的重复项 [英] Find the duplicates among the first string elements in a array of strings
本文介绍了在字符串数组中查找第一个字符串元素中的重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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());
这篇关于在字符串数组中查找第一个字符串元素中的重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文