问题描述
我有许多存储例如words..for一个字符串生成器,我做了
I have a string builder which stores many words..for example, i did
StringBuilder builder = new StringBuilder();
builder.Append(reader.Value);
现在,建设者包含字符串
now, builder contains string as
" india is a great great country and it has many states and territories".. it contains many paragraphs.
我想每个字应该是唯一的再presented和字数。例如,
I want that each word should be unique represented and its word count. example,
india: 1
great: 2
country: 1
and: 2
此外,这一结果应保存在一个Excel文件。但我没有得到的结果。
Also, this result should be saved in a excel file. But I am not getting the result.
我在谷歌搜索,但我通过LINQ或书面文字本身得到它。能否请您帮助我。我是一个初学者。
I searched in google, but i am getting it by linq or by writing the words itself. Can you please help me out. I am a beginner.
推荐答案
您可以使用的LINQ
来实现它。尝试这样的事情。
You can use Linq
to achieve it. Try something like this.
var result = from word in builder.Split(' ')
group word by word into g
select new { Word = g.Key, Count = g.Count() };
您还可以将此结果转换成字典对象,像这样
You can also convert this result into Dictionary object like this
Dictionary<string, int> output = result.ToDictionary(a => a.Word, a => a.Count);
所以,在这里产量将每个项目包含字
的关键,它的计数
的值。
这篇关于从字符串生成器计数的话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!