C#判斷字符編碼的方法總結(jié)(六種方法)
本文實例總結(jié)了C#判斷字符編碼的方法。分享給大家供大家參考,具體如下:
方法一
在unicode 字符串中,中文的范圍是在4E00..9FFF:CJK Unified Ideographs。
通過對字符的unicode編碼進(jìn)行判斷來確定字符是否為中文。
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> /// 給定一個字符串,判斷其是否只包含有漢字 /// </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> /// 判斷一個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> /// 判斷一個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> /// 判斷一個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#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計入門教程》及《C#程序設(shè)計之線程使用技巧總結(jié)》
希望本文所述對大家C#程序設(shè)計有所幫助。
欄 目:C#教程
下一篇:C#實現(xiàn)XML與實體類之間相互轉(zhuǎn)換的方法(序列化與反序列化)
本文標(biāo)題:C#判斷字符編碼的方法總結(jié)(六種方法)
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6443.html
您可能感興趣的文章
- 01-10C#實現(xiàn)實體類與字符串互相轉(zhuǎn)換的方法
- 01-10C#實現(xiàn)判斷當(dāng)前操作用戶管理角色的方法
- 01-10WinForm判斷關(guān)閉事件來源于用戶點(diǎn)擊右上角“關(guān)閉”按鈕的方法
- 01-10http圖片上傳安全性問題 根據(jù)ContentType (MIME) 判斷其實不準(zhǔn)確、不
- 01-10C#中Json字符串的各種應(yīng)用類實例講解
- 01-10VS中C#讀取app.config數(shù)據(jù)庫配置字符串的三種方法
- 01-10C#實現(xiàn)過濾sql特殊字符的方法集合
- 01-10C#判斷三角形的類型
- 01-10輕松學(xué)習(xí)C#的運(yùn)算符
- 01-10輕松學(xué)習(xí)C#的String類


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實例總結(jié)
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個骰子