class DictionaryDemo
{
static Dictionary<string,int> CountWords(string text)
{
Dictionary<string,int> frequencies;
frequencies = new Dictionary<string,int>();
string[] words = Regex.Split(text, @"\W+");
foreach (string word in words)
{
if (frequencies.ContainsKey(word))
{
frequencies[word]++;
}
else
{
frequencies[word] = 1;
}
}
return frequencies;
}
static void Main()
{
string text = @"Do you like green eggs and ham?
I do not like them, Sam-I-am.
I do not like green eggs and ham.";
Dictionary<string, in
深入理解 c# 第三章 用Dictionary来统计文本中的单词数
最新推荐文章于 2023-10-03 16:15:06 发布
本文介绍如何利用C#中的Dictionary<TKey, TValue>数据结构来统计文本中每个单词出现的次数。通过创建字符串到整数的映射,对文本进行正则表达式分解并遍历映射来更新单词计数,最后展示结果。"
104182276,8724200,AOP增强执行顺序与XML配置详解,"['AOP', 'Spring框架', 'XML配置', '编程概念', '面向切面编程']
摘要由CSDN通过智能技术生成