[Reprint] C # How to judge whether a character is a Chinese character,

Source: Internet
Author: User

[Reprint] C # How to judge whether a character is a Chinese character,

Support and respect originality! Address: http://jingyan.baidu.com/article/2c8c281deb79ed0008252af1.html

 

There are three methods to determine whether a character is a Chinese character. 1st types are judged by ASCII code, 2nd types are determined by the UNICODE encoding range of Chinese characters, and 3rd types are determined by regular expressions. The following describes the specific methods.

 

1. Identify with ASCII code

In the ASCII code table, the English range is 0-127, and the Chinese character is greater than 127. The specific code is as follows:

 

1 /// <summary> 2 // use the ASCII code range to determine whether the character is a Chinese character. 3 /// </summary> 4 /// <param name = "text"> waiting judge character or string </param> 5 // <returns> true: is a Chinese character; false: Not </returns> 6 public bool CheckStringChinese (string text) 7 {8 bool res = false; 9 foreach (char t in text) 10 {11 if (int) t> 127) 12 res = true; 13} 14 return res; 15}
View Code

 

Call method: CheckStringChinese ("not Chinese character ");

 

 

2. Use the UNICODE encoding range of Chinese characters to determine

The UNICODE encoding range of Chinese characters is 4e00-9fbb. The specific code is as follows:

 

1 // <summary> 2 // use the UNICODE encoding range to determine whether the character is a Chinese character. 3 /// </summary> 4 /// <param name = "text"> waiting judge character or string </param> 5 // <returns> true: is a Chinese character; false: Not </returns> 6 public bool CheckStringChineseUn (string text) 7 {8 bool res = false; 9 foreach (char t in text) 10 {11 if (t> = 0x4e00 & t <= 0x9fbb) 12 {13 res = true; 14 break; 15} 16} 17 return res; 18}
View Code

 

Call method: CheckStringChineseUn ("not Chinese character ");

 

 

  3. Use regular expressions to determine

Regular expressions are used to determine the UNICODE encoding range of Chinese characters. The specific code is as follows:

 

1 // <summary> 2 // use a regular expression to determine whether a character is a Chinese character. 3 // </summary> 4 // <param name = "text"> to be determined character or string </param> 5 // <returns> true: is a Chinese character; false: Not </returns> 6 public bool CheckStringChineseReg (string text) 7 {8 return System. text. regularExpressions. regex. isMatch (text, @ "[\ u4e00-\ u9fbb] + $"); 9}
View Code

 

Call method: CheckStringChineseReg ("not Chinese character ");

 

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.