1.簡介
之前做漢字轉拼音是使用各種枚舉的方式,將各種情況列舉,這種方式出錯的機率很大,經常對不上號。(如果你想了解更多:http://www.cnblogs.com/islands/articles/1231613.html
后來在度娘的過程中,發現了其實微軟早有漢字轉拼音的組件dll庫,里面估計是封裝了windows系統下輸入法的漢字精華,所以轉化過程中相對要准確的多。而我在做Xamarin.iOS的過程中,由於要對聯系人進行首字母索引,因此使用到了它。
官網了解:http://www.microsoft.com/zh-cn/download/confirmation.aspx?id=15251
使用案例:http://blog.163.com/kunkun0921@126/blog/static/169204332201210735848402/
CSDN下載:http://download.csdn.net/detail/newxdlysk/4293850
2.主要功能
1)支持獲取簡體中文字符的常用屬性:拼音,多音字,同音字,筆畫數
2)繁簡中文互相轉化
注:雖然很方便,但檢索過程中的時間消耗還是挺長的;還有就是一些存在翻譯爭議的地區名稱會跟目前我們拼音有所出入:如廣州(Canton/Guangzhuo).
3.簡單使用(首字母的獲取)
public string Convert (string chr) { try{ if (chr.Length != 0) { StringBuilder fullSpell = new StringBuilder (); for (int i=0; i<chr.Length; i++) { bool isChineses = ChineseChar.IsValidChar (chr [i]); if (isChineses) { ChineseChar chineseChar = new ChineseChar (chr [i]); foreach (string value in chineseChar.Pinyins) { if (!string.IsNullOrEmpty (value)) { fullSpell.Append (value.Remove(value.Length - 1, 1)); break; } } }else { fullSpell.Append(chr[i]); } } return fullSpell.ToString().ToUpper(); } }catch(Exception e){ Console.WriteLine("全拼轉化出錯!"+e.Message); } return string.Empty; } /// <summary> /// 獲取首字母 /// </summary> public string SubFirstLetter(string chr) { try{ chr = chr.Substring(0,1); if (chr.Length != 0) { StringBuilder fullSpell = new StringBuilder (); for (int i=0; i<chr.Length; i++) { bool isChineses = ChineseChar.IsValidChar (chr [i]); if (isChineses) { ChineseChar chineseChar = new ChineseChar (chr [i]); foreach (string value in chineseChar.Pinyins) { if (!string.IsNullOrEmpty (value)) { fullSpell.Append (value.Remove(value.Length - 1, 1)); break; } } }else { fullSpell.Append(chr[i]); } } return fullSpell.ToString().Substring(0,1).ToUpper(); } }catch(Exception e){ Log.Error("首字母轉化出錯!"+e.Message); } return string.Empty; } }