C#判断字符编码的方法总结(六种方法)
本文实例总结了C#判断字符编码的方法。分享给大家供大家参考,具体如下:
方法一
在unicode字符串中,中文的范围是在4E00..9FFF:CJKUnifiedIdeographs。
通过对字符的unicode编码进行判断来确定字符是否为中文。
protectedboolIsChineseLetter(stringinput,intindex) { intcode=0; intchfrom=Convert.ToInt32("4e00",16);//范围(0x4e00~0x9fff)转换成int(chfrom~chend) intchend=Convert.ToInt32("9fff",16); if(input!="") { code=Char.ConvertToUtf32(input,index);//获得字符串input中指定索引index处字符unicode编码 if(code>=chfrom&&code<=chend) { returntrue;//当code在中文范围内返回true } else { returnfalse;//当code不在中文范围内返回false } } returnfalse; }
方法二:
publicboolIsChina(stringCString) { boolBoolValue=false; for(inti=0;i<CString.Length;i++) { if(Convert.ToInt32(Convert.ToChar(CString.Substring(i,1)))<Convert.ToInt32(Convert.ToChar(128))) { BoolValue=false; } else { returnBoolValue=true; } } returnBoolValue; }
方法三:
///<summary> ///判断句子中是否含有中文宁夏大学张冬zd4004.blog.163.com ///</summary> ///<param>字符串</param> publicboolWordsIScn(stringwords) { stringTmmP; for(inti=0;i<words.Length;i++) { TmmP=words.Substring(i,1); byte[]sarr=System.Text.Encoding.GetEncoding("gb2312").GetBytes(TmmP); if(sarr.Length==2) { returntrue; } } returnfalse; }
方法四:
for(inti=0;i<s.length;i++) { Regexrx=newRegex("^[/u4e00-/u9fa5]$"); if(rx.IsMatch(s[i])) //是 else //否 }
正解!
/u4e00-/u9fa5汉字的范围。
^[/u4e00-/u9fa5]$汉字的范围的正则
方法五
unicodeencodingunicodeencoding=newunicodeencoding(); byte[]unicodebytearray=unicodeencoding.getbytes(inputstring); for(inti=0;i<unicodebytearray.length;i++) { i++; //如果是中文字符那么高位不为0 if(unicodebytearray[i]!=0) { } ……
方法六
///<summary> ///给定一个字符串,判断其是否只包含有汉字 ///</summary> ///<paramname="testStr"></param> ///<returns></returns> publicboolIsOnlyContainsChinese(stringtestStr) { char[]words=testStr.ToCharArray(); foreach(charwordinwords) { if(IsGBCode(word.ToString())||IsGBKCode(word.ToString()))//itisaGB2312orGBKchineseword { continue; } else { returnfalse; } } returntrue; } ///<summary> ///判断一个word是否为GB2312编码的汉字 ///</summary> ///<paramname="word"></param> ///<returns></returns> privateboolIsGBCode(stringword) { byte[]bytes=Encoding.GetEncoding("GB2312").GetBytes(word); if(bytes.Length<=1)//ifthereisonlyonebyte,itisASCIIcodeorothercode { returnfalse; } else { bytebyte1=bytes[0]; bytebyte2=bytes[1]; if(byte1>=176&&byte1<=247&&byte2>=160&&byte2<=254)//判断是否是GB2312 { returntrue; } else { returnfalse; } } } ///<summary> ///判断一个word是否为GBK编码的汉字 ///</summary> ///<paramname="word"></param> ///<returns></returns> privateboolIsGBKCode(stringword) { byte[]bytes=Encoding.GetEncoding("GBK").GetBytes(word.ToString()); if(bytes.Length<=1)//ifthereisonlyonebyte,itisASCIIcode { returnfalse; } else { bytebyte1=bytes[0]; bytebyte2=bytes[1]; if(byte1>=129&&byte1<=254&&byte2>=64&&byte2<=254)//判断是否是GBK编码 { returntrue; } else { returnfalse; } } } ///<summary> ///判断一个word是否为Big5编码的汉字 ///</summary> ///<paramname="word"></param> ///<returns></returns> privateboolIsBig5Code(stringword) { byte[]bytes=Encoding.GetEncoding("Big5").GetBytes(word.ToString()); if(bytes.Length<=1)//ifthereisonlyonebyte,itisASCIIcode { returnfalse; } else { bytebyte1=bytes[0]; bytebyte2=bytes[1]; if((byte1>=129&&byte1<=254)&&((byte2>=64&&byte2<=126)||(byte2>=161&&byte2<=254)))//判断是否是Big5编码 { returntrue; } else { returnfalse; } } }
更多关于C#相关内容感兴趣的读者可查看本站专题:《C#中XML文件操作技巧汇总》、《C#常见控件用法教程》、《WinForm控件用法总结》、《C#数据结构与算法教程》、《C#面向对象程序设计入门教程》及《C#程序设计之线程使用技巧总结》
希望本文所述对大家C#程序设计有所帮助。