判断输入字符中包含汉字数目

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Text;
 7 using System.Windows.Forms;
 8 using System.Text.RegularExpressions;
 9 using System.Collections;
10 
11 namespace Test19
12 {
13     public partial class Form1 : Form
14     {
15         public Form1()
16         {
17             InitializeComponent();//初始化窗体
18         }
19         private void button1_Click(object sender, EventArgs e)//button1的单击事件
20         {
21             ArrayList itemList = new ArrayList();//定义一个空数组
22             CharEnumerator CEnumerator = textBox1.Text.GetEnumerator();//将textBox1的Text中的字符串给CEnumerator
23             Regex regex = new Regex("^[u4e00-u9fa5]{0,}$");//定义一个正则表达式,这里是只允许输入汉字的意思。
24             while (CEnumerator.MoveNext())//递增索引,指向下一个字符,如果没有下一个就停止循环。
25             {
26                 if (regex.IsMatch(CEnumerator.Current.ToString(), 0))//如果CEnumerator的当前字符符合regex这个规则,那么就把这个字符插入到itemlist里面。
27                     itemList.Add(CEnumerator.Current.ToString());
28                 textBox2.Text = itemList.Count.ToString();//将itemList的项数显示到textBox2里面。
29             }
30         }
31     }
32 }

原文地址:https://www.cnblogs.com/liuyaozhi/p/4915629.html