This paper summarizes the method of C # judgment character encoding. Share to everyone for your reference, specific as follows:
Method One
In a Unicode string, the Chinese range is in 4E00. 9FFF:CJK Unified ideographs.
Determines whether a 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"); The range (0X4E00~0X9FFF) is converted to int (chfrom~chend)
int chend = Convert.ToInt32 ("9fff");
if (input!= "")
{
code = CHAR.CONVERTTOUTF32 (input, index); Gets the index of the specified index in string input Unicode encoding
if (code >= chfrom && Code <= chend)
{return
true; When the code returns true in the Chinese
range
, else {return
false; Return FALSE if code is not in Chinese range}
Method Two:
public bool Ischina (string CString)
{
bool boolvalue = false;
for (int i = 0; i < cstring.length i++)
{
if (Convert.ToInt32 (Convert.tochar (Cstring.substring (i, 1)) < Co Nvert. ToInt32 (Convert.tochar (128)))
{
boolvalue = false;
}
else
{return
boolvalue = true;
}
}
return boolvalue;
}
Method Three:
<summary>
///Whether the sentence contains Chinese Ningxia University Zhang Dong zd4004.blog.163.com
///</summary>
///< param > String </param> public
bool Wordsiscn (string words)
{
string tmmp;
for (int i = 0; i < words. Length; i++)
{
TMMP = words. Substring (i, 1);
byte[] Sarr = System.Text.Encoding.GetEncoding ("gb2312"). GetBytes (TMMP);
if (Sarr. Length = = 2)
{return
true;
}
}
return false;
}
Method Four:
for (int i=0; i<s.length; i++)
{
regex rx = new Regex ("^[/u4e00-/u9fa5]$");
if (Rx. IsMatch (S[i]))
//is
else
//No
}
Positive Solution!
/u4e00-/u9fa5 the range of Chinese characters.
Regular ^[/u4e00-/u9fa5]$ of the range of Chinese characters
Method Five
UnicodeEncoding unicodeencoding = new UnicodeEncoding ();
byte [] Unicodebytearray = Unicodeencoding.getbytes (inputstring);
for (int i = 0; i < unicodebytearray.length; i++)
{
i++;
If it is a Chinese character so high is not 0
if (Unicodebytearray[i]!= 0)
{
}
...
Method Six
<summary>///given a string to determine whether it contains only Chinese characters///</summary>///<param name= "Teststr" ></param>///<
Returns></returns> public bool Isonlycontainschinese (string teststr) {char[] words = Teststr.tochararray (); foreach (char word in words) {if Isgbcode (word. ToString ()) | | Isgbkcode (Word.
ToString ())//It is a GB2312 or GBK Chinese word {continue;
else {return false;
} return true; ///<summary>///To determine if Word is a GB2312 encoded kanji///</summary>///<param name= "word" ></param>/// ;returns></returns> private bool Isgbcode (string word) {byte[] bytes = encoding.getencoding ("GB2312").
GetBytes (word); if (bytes.
Length <= 1//If there is only one byte, it's 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>///to determine if Word is a GBK-encoded kanji///</summary>///<param name= "word" ></param>///& lt;returns></returns> private bool Isgbkcode (string word) {byte[] bytes = encoding.getencoding ("GBK"). GetBytes (Word.
ToString ()); if (bytes.
Length <= 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 >= $ && byte2 <= 254)//Determine if GBK code {
return true;
else {return false; }}///<summary>///to determine if Word is a BIG5-encoded kanji///</summary>///<param name= "word" ></param>/// <returns></returns> private bool Isbig5code (string word) {byte[] bytes = encoding.getencoding ("Big5"). GetBytes (Word.
ToString ()); if (bytes. Length <= 1//If there is only one byte, it's ASCII code {return false;
else {byte byte1 = bytes[0];
byte Byte2 = bytes[1]; if ((byte1 >= 129 && byte1 <= 254) && (byte2 >= && byte2 <= 126) | | (Byte2 >= 161 && byte2 <= 254))
//Determine if it is BIG5 encoding {return true;
else {return false;
}
}
}
Read more about C # Interested readers can view the site topics: "C # XML file Operation Tips Summary", "C # Common control usage Tutorial", "WinForm Control Usage Summary", "C # Data structure and algorithm tutorial", "C # object-oriented Program design Introductory Course" and "C # A summary of thread usage tips for programming
I hope this article will help you with C # programming.