//统计字符串中字符出现次数,不区分大小写
string str = "Welcome to Chinaword";
string lowerstr=str.ToLower();
Dictionary dic = new Dictionary();
foreach(char ch in lowerstr)
{
if (dic.ContainsKey(ch))
{
dic[ch]++;
}
else
{
dic[ch] = 1;
}
}
//方法一:直接遍历Keys.
foreach(char ch in dic.Keys)
{
Console.WriteLine("{0}出现的次数为{1}",ch,dic[ch]);
}
//方法二:遍历dic中的键值对
foreach (KeyValuePair ch in dic)
{
Console.WriteLine("{0}出现的次数为{1}",ch.Key,ch.Value);
}