asp.net method to determine if a string is Chinese

  • 2020-06-01 09:20:04
  • OfStack

Any character takes two bytes in the unicode encoding.
However, Chinese and English characters can be represented by 1 byte, while Chinese characters can be represented by 2 bytes.
In this way, the English character takes up an extra byte in the unicode encoding, which is replaced by 0.
If the first byte of a character in unicode is 0, it is an English character. If it's not zero, it's probably in many languages other than English, not just Chinese.


View Code 
public bool CheckChinese(string str)
        {
            bool flag = false;
            UnicodeEncoding a = new UnicodeEncoding();
            byte[] b = a.GetBytes(str);
            for(int i=0;i<b.Length;i++)
            {
                i++;
                if (b[i] != 0)
                {
                    flag = true;
                }
                else
                {
                    flag = false;
                }
            }
            return flag;
        }


Related articles: