There are usually three ways to judge whether a character is a Chinese character. The first method is judged by ASCII code, the second method is judged by the UNICODE encoding range of Chinese characters, and the third method is judged by regular expression. The following is the specific method.
- 1. Judging by ASCII code
In the ASCII code table, the range of English is 0-127, while the range of Chinese characters is greater than 127. The specific codes are as follows:
string text = "是不是汉字,ABC,柯乐义";
for (int i = 0; i < text.Length; i++)
{
if ((int)text[i] > 127)
Console.WriteLine("是汉字");
else
Console.WriteLine("不是汉字");
}
- 2. Judging by the UNICODE encoding range of Chinese characters
The UNICODE encoding range of Chinese characters is 4e00-9fbb, and the specific codes are as follows:
string text = "是不是汉字,ABC,keleyi.com";
char[] c = text.ToCharArray();
for (int i = 0; i < c.Length;i++)
if (c[i] >= 0x4e00 && c[i] <= 0x9fbb)
Console.WriteLine("是汉字");
else
Console.WriteLine("不是汉字");
- 3. Judging with regular expressions
Judging with regular expressions also uses the UNICODE encoding range of Chinese characters. The specific codes are as follows:
string text = "是不是汉字,ABC,keleyi.com";
for (int i = 0; i < text.Length; i++)
{
if (Regex.IsMatch(text[i].ToString(), @"[\u4e00-\u9fbb]+{1}quot;))
Console.WriteLine("是汉字");
else
Console.WriteLine("不是汉字");
}