gpt4 book ai didi

c# - 计算字符串生成器中的单词

转载 作者:行者123 更新时间:2023-12-02 22:03:00 26 4
gpt4 key购买 nike

我有一个存储很多单词的字符串生成器..例如,我做了

StringBuilder builder = new StringBuilder();
builder.Append(reader.Value);

现在,构建器包含字符串作为

" india is a great great country and it has many states and territories".. it contains many paragraphs.

我希望每个词都应该是唯一的,并且它的字数应该是唯一的。例如,

india: 1
great: 2
country: 1
and: 2

此外,此结果应保存在 excel 文件中。但我没有得到结果。

我在 google 中搜索过,但我是通过 linq 或自己编写单词来获取它的。你能帮帮我吗?我是初学者。

最佳答案

您可以使用Linq 来实现它。尝试这样的事情。

var result = from word in builder.Split(' ')
group word by word into g
select new { Word = g.Key, Count = g.Count() };

您也可以像这样将此结果转换为 Dictionary 对象

Dictionary<string, int> output = result.ToDictionary(a => a.Word, a => a.Count);

因此这里输出中的每个项目都将包含 Word 作为 Key,它的 Count 作为值。

关于c# - 计算字符串生成器中的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16577586/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com