C# 相同字符在字符串中出现的次数 只能输入字母和数字(winform)

public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string s = txtInput.Text.Trim().ToString();
            CaculateCoutTimes(s);
        }

        private void CaculateCoutTimes(string s)
        {
            Dictionary<char, int> dic = new Dictionary<char, int>();
            char[] chardata = s.ToCharArray();
            string value = "";
            foreach (char c in chardata)
            {
                if (!dic.ContainsKey(c))
                {
                    dic.Add(c, 1);
                }
                else
                {
                    dic[c] = dic[c] + 1;
                }
            }
            foreach (KeyValuePair<char, int> item in dic)
            {
                if (item.Value > 1)
                {
                    value += item.Key + "出现" + item.Value + "

";
                }
         
            }
            lblTxt.Text = value;
            Console.ReadLine();
        }

        private void txtInput_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= 'a' && e.KeyChar <= 'z') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z')
               || (e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == 8))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtInput.Text = "";
            lblTxt.Text = "";
        }
    }

引用http://blog.csdn.net/xl419/article/details/40866083  还有其他的几种判断的方法

以及涉及到HashTable、HashSet和Dictionary的区别(http://www.cnblogs.com/akwwl/p/3680376.html)

原文地址:https://www.cnblogs.com/cylblogs/p/6878793.html