How to judge a character in C # as a kanji

Source: Internet
Author: User

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

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.