Determines the number of Chinese characters in the input string and the number of Chinese characters in the string.

Source: Internet
Author: User

Determines the number of Chinese characters in the input string and the number of Chinese characters in the string.

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 (); // initialize the Form 18} 19 private void button#click (ob Ject sender, EventArgs e) // click event 20 {21 ArrayList itemList = new ArrayList (); // defines an empty array 22 CharEnumerator CEnumerator = textBox1.Text. getEnumerator (); // give the string in textBox1 Text to CEnumerator23 Regex regex = new Regex ("^ [\ u4e00-\ u9fa5] {0,} $ "); // define a regular expression. Here, only Chinese characters can be entered. 24 while (CEnumerator. MoveNext () // incrementing index, pointing to the next character. If there is no next character, the loop is stopped. 25 {26 if (regex. isMatch (CEnumerator. current. toString (), 0) // if the current character of CEnumerator complies with the regex rule, insert this character into itemlist. 27 itemList. Add (CEnumerator. Current. ToString (); 28 textBox2.Text = itemList. Count. ToString (); // display the number of itemList items in textBox2. 29} 30} 31} 32}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.