SoFunction
Updated on 2025-03-07

Summary of C#'s method of judging character encoding (six methods)

This article summarizes the method of judging character encoding in C#. Share it for your reference, as follows:

Method 1

In unicode strings, the Chinese range is in 4E00..9FFF:CJK Unified Ideographs.
Determine whether the character is Chinese by judging the unicode encoding of the character.

protected bool  IsChineseLetter(string input,int index)
{
    int code = 0;
    int chfrom = Convert.ToInt32("4e00", 16);  //Convert range (0x4e00~0x9ff) to int (chfrom~chend)    int chend = Convert.ToInt32("9fff", 16);
    if (input != "")
    {
       code = Char.ConvertToUtf32(input, index);  //Get the character unicode encoding at the specified index in the string input      if (code >= chfrom && code <= chend)
      {
         return true;   //Return true when the code is in Chinese range       }
      else
      {
         return false ;  //Return false when the code is not in Chinese range       }
     }
     return false;
}

Method 2:

public bool IsChina(string CString)
{
   bool BoolValue = false;
   for (int i = 0; i < ; i++)
   {
     if (Convert.ToInt32(((i, 1))) < Convert.ToInt32((128)))
     {
       BoolValue = false;
     }
     else
     {
       return BoolValue = true;
     }
   }
   return BoolValue;
}

Method 3:

/// &lt;summary&gt;
/// Determine whether the sentence contains Chinese: Ningxia University Zhang Dong./// &lt;/summary&gt;
/// <param >Strings</param>public bool WordsIScn(string words)
{
  string TmmP;
  for (int i = 0; i &lt; ; i++)
  {
    TmmP = (i, 1);
    byte[] sarr = ("gb2312").GetBytes(TmmP);
    if ( == 2)
    {
      return true;
    }
  }
  return false;
}

Method 4:

for (int i=0; i&lt;; i++)
{
Regex rx = new Regex("^[/u4e00-/u9fa5]$");
if ((s[i]))
// yeselse
// no}

Correct solution!

/u4e00-/u9fa5 The range of Chinese characters.
^[/u4e00-/u9fa5]$ Regularity of the range of Chinese characters

Method 5

unicodeencoding unicodeencoding = new unicodeencoding();
byte [] unicodebytearray = ( inputstring );
for( int i = 0; i &lt; ; i++ )
{
i++;
//If it is a Chinese character, the high position will not be 0if ( unicodebytearray[i] != 0 )
{
}
……

Method 6

/// &lt;summary&gt;
/// Given a string, determine whether it contains only Chinese characters/// &lt;/summary&gt;
/// &lt;param name="testStr"&gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
public bool IsOnlyContainsChinese(string testStr)
{
  char[] words = ();
  foreach (char word in words)
  {
    if ( IsGBCode(()) || IsGBKCode(()) ) // it is a GB2312 or GBK chinese word
    {
      continue;
    }
    else
    {
      return false;
    }
  }
  return true;
}
/// &lt;summary&gt;
/// Determine whether a word is a Chinese character encoded by GB2312/// &lt;/summary&gt;
/// &lt;param name="word"&gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
private bool IsGBCode(string word)
{
  byte[] bytes = ("GB2312").GetBytes(word);
  if ( &lt;= 1) // if there is only one byte, it is ASCII code or other code
  {
    return false;
  }
  else
  {
    byte byte1 = bytes[0];
    byte byte2 = bytes[1];
    if (byte1 &gt;= 176 &amp;&amp; byte1 &lt;= 247 &amp;&amp; byte2 &gt;= 160 &amp;&amp; byte2 &lt;= 254)  //Judge whether it is GB2312    {
      return true;
    }
    else
    {
      return false;
    }
  }
}
/// &lt;summary&gt;
/// Determine whether a word is a GBK-encoded Chinese character/// &lt;/summary&gt;
/// &lt;param name="word"&gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
private bool IsGBKCode(string word)
{
  byte[] bytes = ("GBK").GetBytes(());
  if ( &lt;= 1) // if there is only one byte, it is ASCII code
  {
    return false;
  }
  else
  {
    byte byte1 = bytes[0];
    byte byte2 = bytes[1];
    if ( byte1 &gt;= 129 &amp;&amp; byte1 &lt;= 254 &amp;&amp; byte2 &gt;= 64 &amp;&amp; byte2 &lt;= 254)   //Judge whether it is GBK encoding    {
      return true;
    }
    else
    {
      return false;
    }
  }
}
/// &lt;summary&gt;
/// Determine whether a word is a Big5 coded Chinese character/// &lt;/summary&gt;
/// &lt;param name="word"&gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
private bool IsBig5Code(string word)
{
  byte[] bytes = ("Big5").GetBytes(());
  if ( &lt;= 1) // if there is only one byte, it is ASCII code
  {
    return false;
  }
  else
  {
    byte byte1 = bytes[0];
    byte byte2 = bytes[1];
    if ( (byte1 &gt;= 129 &amp;&amp; byte1 &lt;= 254) &amp;&amp; ((byte2 &gt;= 64 &amp;&amp; byte2 &lt;= 126) || (byte2 &gt;= 161 &amp;&amp; byte2 &lt;= 254)) )   //Judge whether it is Big5 encoding    {
      return true;
    }
    else
    {
      return false;
    }
  }
}

For more information about C# related content, please check out the topic of this site:Summary of XML file operation skills in C#》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming

I hope this article will be helpful to everyone's C# programming.