using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Text.RegularExpressions; using System.Collections; namespace VA14.___小结 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //中文出现的次数 string str = "这是一行新,的语句SQL新新ee新"; string pattern = @"[^\u4e00-\u9fa5]"; Regex regx = new Regex(pattern ); string chnstr = regx.Replace(str,""); //只包含中文 Hashtable ht = new Hashtable(); for (int i = 0; i < chnstr.Length; i++) { int val = 1; if (ht.ContainsKey(chnstr[i])) //判断Hashtable Key 存在 chnstr[i]代表的值否 { val = Convert.ToInt32(ht[chnstr[i]]); //根据 Key 转 Values val++; } ht[chnstr[i]] = val; } textBox1.Text += "原:" + str + "\r\n"; textBox1.Text += "现:" + chnstr + "\r\n"; textBox1.Text += "========================================================\r\n"; foreach (DictionaryEntry myboj in ht) { textBox1.Text += "键值为:" + myboj.Key + ",值为:" + myboj.Value + "\r\n"; } } private void Form1_Load(object sender, EventArgs e) { } private void textBox1_TextChanged(object sender, EventArgs e) { } } } |
|
来自: 时间剧毒 > 《Hashtable》