C#判斷字符編碼的方法總結(jié)(六種方法)
本文實(shí)例總結(jié)了C#判斷字符編碼的方法。分享給大家供大家參考,具體如下:
方法一
在unicode 字符串中,中文的范圍是在4E00..9FFF:CJK Unified Ideographs。
通過(guò)對(duì)字符的unicode編碼進(jìn)行判斷來(lái)確定字符是否為中文。
protected bool IsChineseLetter(string input,int index) { int code = 0; int chfrom = Convert.ToInt32("4e00", 16); //范圍(0x4e00~0x9fff)轉(zhuǎn)換成int(chfrom~chend) int chend = Convert.ToInt32("9fff", 16); if (input != "") { code = Char.ConvertToUtf32(input, index); //獲得字符串input中指定索引index處字符unicode編碼 if (code >= chfrom && code <= chend) { return true; //當(dāng)code在中文范圍內(nèi)返回true } else { return false ; //當(dāng)code不在中文范圍內(nèi)返回false } } return false; }
方法二:
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))) < Convert.ToInt32(Convert.ToChar(128))) { BoolValue = false; } else { return BoolValue = true; } } return BoolValue; }
方法三:
/// <summary> /// 判斷句子中是否含有中文 寧夏大學(xué) 張冬 zd4004.blog.163.com /// </summary> /// <param >字符串</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; }
方法四:
for (int i=0; i<s.length; i++) { Regex rx = new Regex("^[/u4e00-/u9fa5]$"); if (rx.IsMatch(s[i])) // 是 else // 否 }
正解!
/u4e00-/u9fa5 漢字的范圍。
^[/u4e00-/u9fa5]$ 漢字的范圍的正則
方法五
unicodeencoding unicodeencoding = new unicodeencoding(); byte [] unicodebytearray = unicodeencoding.getbytes( inputstring ); for( int i = 0; i < unicodebytearray.length; i++ ) { i++; //如果是中文字符那么高位不為0 if ( unicodebytearray[i] != 0 ) { } ……
方法六
/// <summary> /// 給定一個(gè)字符串,判斷其是否只包含有漢字 /// </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> /// 判斷一個(gè)word是否為GB2312編碼的漢字 /// </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 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) //判斷是否是GB2312 { return true; } else { return false; } } } /// <summary> /// 判斷一個(gè)word是否為GBK編碼的漢字 /// </summary> /// <param name="word"></param> /// <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 >= 64 && byte2 <= 254) //判斷是否是GBK編碼 { return true; } else { return false; } } } /// <summary> /// 判斷一個(gè)word是否為Big5編碼的漢字 /// </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 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)) ) //判斷是否是Big5編碼 { return true; } else { return false; } } }
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#中XML文件操作技巧匯總》、《C#常見(jiàn)控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》及《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
解析OpenXml?Pptx的邊框虛線轉(zhuǎn)為WPF的邊框虛線問(wèn)題
這篇文章主要介紹了OpenXml?Pptx的邊框虛線轉(zhuǎn)為WPF的邊框虛線,在文中用PPTX的7種直線,分別設(shè)置7種能夠設(shè)置的虛線類型,具體實(shí)例代碼跟隨小編一起看看吧2021-12-12C#實(shí)現(xiàn)HTML轉(zhuǎn)WORD及WORD轉(zhuǎn)PDF的方法
這篇文章主要介紹了C#實(shí)現(xiàn)HTML轉(zhuǎn)WORD及WORD轉(zhuǎn)PDF的方法,涉及C#實(shí)現(xiàn)HTML、WORD及PDF等文件格式轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2015-09-09C#導(dǎo)出網(wǎng)站功能實(shí)例代碼講解
這篇文章主要介紹了C#導(dǎo)出網(wǎng)站功能實(shí)例代碼,需要的朋友可以參考下2015-10-10C#?WPF中RadioButton控件的用法及應(yīng)用場(chǎng)景
在WPF應(yīng)用程序中,RadioButton控件是一種常用的用戶界面元素,本文主要介紹了C#?WPF中RadioButton控件的用法及應(yīng)用場(chǎng)景,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03c# 共享狀態(tài)的文件讀寫實(shí)現(xiàn)代碼
開(kāi)發(fā)中有時(shí)會(huì)遇到要對(duì)文件進(jìn)行共享狀態(tài)的讀寫操作,代碼如下,需要的朋友可以參考下2012-06-06C#實(shí)現(xiàn)更快讀寫超級(jí)大文件的方法詳解
這篇文章主要來(lái)和大家介紹一下C#實(shí)現(xiàn)更快讀寫超級(jí)大文件的方法,文中的示例代碼簡(jiǎn)潔易懂,對(duì)我們深入了解C#有一定的幫助,快跟隨小編一起學(xué)習(xí)起來(lái)吧2023-06-06WPF應(yīng)用啟動(dòng)慢的問(wèn)題解決
今天碰到一個(gè)奇怪的現(xiàn)象,在某些機(jī)器上,進(jìn)行了系統(tǒng)還原后,WPF應(yīng)用打開(kāi)較慢,約有35s。本文先記錄下該問(wèn)題的解決方案,應(yīng)用啟動(dòng)性能官方文檔中有說(shuō)明,還有搜到的其它方案沒(méi)來(lái)得及測(cè)試,如NGEN update2021-05-05C#之如何實(shí)現(xiàn)多個(gè)子窗體切換效果
這篇文章主要介紹了C#之如何實(shí)現(xiàn)多個(gè)子窗體切換的效果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07