Three ways c can determine whether a character is Chinese or not share of regular expression determination

  • 2020-06-01 10:52:59
  • 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 UNICODE encoding range of 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

Regular expressions are used to judge the UNICODE encoding range of Chinese characters. 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 ");
        }


Related articles: