To determine whether a character is usually three methods, the first one is judged by ASCII code, the second is judged by the UNICODE encoding range of the Chinese character, and the third is judged by the regular expression, the following is the specific method.
1. Use ASCII code to judge
In the ASCII code table, the English range is 0-127, while the kanji is greater than 127,
The specific code is as follows:
String text = "Not kanji, ABC";
for (int i = 0; i < text. Length; i++)
{
if ((int) text[i] > 127)
Console.WriteLine ("is Kanji");
Else
Console.WriteLine ("Not kanji");
}
2, using the UNICODE encoding range of Chinese characters to judge
The UNICODE encoding range of Chinese characters is 4e00-9fbb, the specific code is as follows:
String text = "Not kanji, ABC";
Char[] C = text. ToCharArray ();
for (int i = 0; i < c.length;i++)
if (C[i] >= 0x4e00 && c[i] <= 0x9fbb)
Console.WriteLine ("is Kanji");
Else
Console.WriteLine ("Not kanji");
3. Judging with regular expression
The regular expression is also used to determine the UNICODE encoding range of Chinese characters, the specific code is as follows:
String text = "Not kanji, ABC";
for (int i = 0; i < text. Length; i++)
{
if (Regex.IsMatch (text[i). ToString (), @ "[\u4e00-\u9fbb]+{1}quot;)]
Console.WriteLine ("is Kanji");
Else
Console.WriteLine ("Not kanji");
}
How to judge a character in C # as a kanji