C# 使用字典计算出现次数
我的问题是,我试图从文本框中获取文本体 与“插入名人”共度一天#兴奋#快乐#快乐# 然后我想计算每个标签在正文中出现的次数,可以是任意长度的文本 所以上面会返回这个 兴奋=1 快乐=2 我计划使用字典,但我不确定如何实现对hashtag的搜索并添加到字典中 到目前为止,我只有这些了C# 使用字典计算出现次数,c#,dictionary,C#,Dictionary,我的问题是,我试图从文本框中获取文本体 与“插入名人”共度一天#兴奋#快乐#快乐# 然后我想计算每个标签在正文中出现的次数,可以是任意长度的文本 所以上面会返回这个 兴奋=1 快乐=2 我计划使用字典,但我不确定如何实现对hashtag的搜索并添加到字典中 到目前为止,我只有这些了 string body = txtBody.Text; Dictionary<string, string> dic = new Dictionary<string, strin
string body = txtBody.Text;
Dictionary<string, string> dic = new Dictionary<string, string>();
foreach(char c in body)
{
}
string body=txtBody.Text;
Dictionary dic=新字典();
foreach(主体中的字符c)
{
}
感谢您的帮助这将在一个字符串中找到任何哈希标记,该字符串的格式为哈希后跟一个或多个非空白字符,并创建一个它们与计数的字典 你的意思是词典,是吗
var input = "Spent the day with \"insert famous name\" '#excited #happy #happy";
Dictionary<string, int> dic =
Regex
.Matches(input, @"(?<=\#)\S+")
.Cast<Match>()
.Select(m => m.Value)
.GroupBy(s => s)
.ToDictionary(g => g.Key, g => g.Count());
var input=“与\“插入著名名字\'”共度一天#兴奋#快乐#快乐”;
字典dic=
正则表达式
.Matches(input,@)(?这将查找以哈希形式后跟一个或多个非空白字符的字符串中的任何哈希标记,并根据它们的计数创建一个字典
你的意思是词典,是吗
var input = "Spent the day with \"insert famous name\" '#excited #happy #happy";
Dictionary<string, int> dic =
Regex
.Matches(input, @"(?<=\#)\S+")
.Cast<Match>()
.Select(m => m.Value)
.GroupBy(s => s)
.ToDictionary(g => g.Key, g => g.Count());
var input=“与\“插入著名名字\'”共度一天#兴奋#快乐#快乐”;
字典dic=
正则表达式
.Matches(输入,@)(?这可以通过两种LINQ方法实现:
var text = "Spent the day with <insert famous name> #excited #happy #happy";
var hashtags = text.Split(new[] { ' ' })
.Where(word => word.StartsWith("#"))
.GroupBy(hashtag => hashtag)
.ToDictionary(group => group.Key, group => group.Count());
Console.WriteLine(string.Join("; ", hashtags.Select(kvp => kvp.Key + ": " + kvp.Value)));
这可以通过两种LINQ方法实现:
var text = "Spent the day with <insert famous name> #excited #happy #happy";
var hashtags = text.Split(new[] { ' ' })
.Where(word => word.StartsWith("#"))
.GroupBy(hashtag => hashtag)
.ToDictionary(group => group.Key, group => group.Count());
Console.WriteLine(string.Join("; ", hashtags.Select(kvp => kvp.Key + ": " + kvp.Value)));
@spender应该说,“eg happy”后面的词可以是任何东西,它应该是模拟推特计数器,所以它可以是任何东西#word@spender应该说,#eg happy后面的单词可以是任何东西,它应该是模拟推特计数器,所以它可以是任何单词谢谢你,这对我有用,或者对t他说我已经做了测试,所以进一步的测试应该仍然有效谢谢你,这对我有效,或者我已经做了测试,所以进一步的测试应该仍然有效