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:
/// <summary> /// Determine whether the sentence contains Chinese: Ningxia University Zhang Dong./// </summary> /// <param >Strings</param>public bool WordsIScn(string words) { string TmmP; for (int i = 0; i < ; i++) { TmmP = (i, 1); byte[] sarr = ("gb2312").GetBytes(TmmP); if ( == 2) { return true; } } return false; }
Method 4:
for (int i=0; i<; 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 < ; i++ ) { i++; //If it is a Chinese character, the high position will not be 0if ( unicodebytearray[i] != 0 ) { } ……
Method 6
/// <summary> /// Given a string, determine whether it contains only Chinese characters/// </summary> /// <param name="testStr"></param> /// <returns></returns> 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; } /// <summary> /// Determine whether a word is a Chinese character encoded by GB2312/// </summary> /// <param name="word"></param> /// <returns></returns> private bool IsGBCode(string word) { byte[] bytes = ("GB2312").GetBytes(word); if ( <= 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 >= 176 && byte1 <= 247 && byte2 >= 160 && byte2 <= 254) //Judge whether it is GB2312 { return true; } else { return false; } } } /// <summary> /// Determine whether a word is a GBK-encoded Chinese character/// </summary> /// <param name="word"></param> /// <returns></returns> private bool IsGBKCode(string word) { byte[] bytes = ("GBK").GetBytes(()); if ( <= 1) // if there is only one byte, it is ASCII code { return false; } else { byte byte1 = bytes[0]; byte byte2 = bytes[1]; if ( byte1 >= 129 && byte1 <= 254 && byte2 >= 64 && byte2 <= 254) //Judge whether it is GBK encoding { return true; } else { return false; } } } /// <summary> /// Determine whether a word is a Big5 coded Chinese character/// </summary> /// <param name="word"></param> /// <returns></returns> private bool IsBig5Code(string word) { byte[] bytes = ("Big5").GetBytes(()); if ( <= 1) // if there is only one byte, it is ASCII code { return false; } else { byte byte1 = bytes[0]; byte byte2 = bytes[1]; if ( (byte1 >= 129 && byte1 <= 254) && ((byte2 >= 64 && byte2 <= 126) || (byte2 >= 161 && byte2 <= 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.