[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 ");