首页 > 学院 > 开发设计 > 正文

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

2019-11-14 13:51:44
字体:
来源:转载
供稿:网友
 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 Test1912 {13     public partial class Form1 : Form14     {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中的字符串给CEnumerator23             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 }


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表