C shares three ways to determine if a character is a Chinese character
- 2020-06-01 10:51:28
- OfStack
1. Judge with ASCII code
In the ASCII code table, the range of English is 0-127, while Chinese is greater than 127. The specific code is as follows:
string text = " Is it Chinese? ABC, KeLeYi ";
for (int i = 0; i < text.Length; i++)
{
if ((int)text[i] > 127)
Console.WriteLine(" Is the Chinese character ");
else
Console.WriteLine(" Not Chinese characters ");
}
2. Judge by the UNICODE coding range of Chinese characters
The encoding range of UNICODE for Chinese characters is 4e00-9fbb. The specific codes are as follows:
string text = " Is it Chinese? ABC,keleyi.com";
char[] c = text.ToCharArray();
for (int i = 0; i < c.Length;i++)
if (c[i] >= 0x4e00 && c[i] <= 0x9fbb)
Console.WriteLine(" Is the Chinese character ");
else
Console.WriteLine(" Not Chinese characters ");
3. Use regular expressions
The UNICODE encoding range is also determined by using regular expressions. The specific code is as follows:
string text = " Is it Chinese? ABC . keleyi.com";
for (int i = 0; i < text.Length; i++)
{
if (Regex.IsMatch(text[i].ToString(), @"[\u4e00-\u9fbb]+{1}quot;))
Console.WriteLine(" Is the Chinese character ");
else
Console.WriteLine(" Not Chinese characters ");
}