c# 中文轉(zhuǎn)拼音without CJK
Xamarin寫(xiě)Android程序時(shí),通常要使用按中文首字母分組顯示(如通訊錄) 。
于是需要被迫包含CJK,不過(guò)包含后包肯定是會(huì)變大的,于是。。。。自己寫(xiě)了一個(gè)硬枚舉的中文轉(zhuǎn)拼音的類(lèi)。
原理是這樣的:
public class PinYinUtils { private static readonly Dictionary<string, string> PinYinDict = new Dictionary<string, string> { {"猿", "YUAN"} // 等............ }; /// <summary> /// Return to the first letter /// </summary> /// <param name="word">Chinese word</param> /// <example> /// GetFirstPinyinChar("張三") /// will return "Z" /// Can be used for address book index and so on /// </example> /// <returns></returns> public static string GetFirstPinyinChar(string word) { if (word.Length == 0) return "#"; var firstLetter = word[0].ToString(); if (PinYinDict.ContainsKey(firstLetter)) { return PinYinDict[firstLetter]; } return firstLetter; } /// <summary> /// return the chinese char's pinyin /// </summary> /// <param name="chineseChar"></param> /// <example> /// GetPinYin('福') /// will return "FU" /// </example> /// <returns></returns> public static string GetPinYin(char chineseChar) { var str = chineseChar.ToString(); if (PinYinDict.ContainsKey(str)) { return PinYinDict[str]; } return null; } /// <summary> /// Get the phonetic abbreviation for Chinese char /// </summary> /// <param name="chineseChar"></param> /// <example> /// GetShortPinYin('福') /// will return "F" /// </example> /// <returns></returns> public static string GetShortPinYin(char chineseChar) { var str = chineseChar.ToString(); if (PinYinDict.ContainsKey(str)) { var first = PinYinDict[str].FirstOrDefault(); if (first == 0) return null; return first.ToString(); } return null; } }
源碼:
https://github.com/chsword/PinYinUtil/blob/master/PinYinUtils.cs
GITHUB:https://github.com/chsword/PinYinUtil
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持我們!
欄 目:C#教程
下一篇:分享兩種實(shí)現(xiàn)Winform程序的多語(yǔ)言支持的多種解決方案
本文標(biāo)題:c# 中文轉(zhuǎn)拼音without CJK
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5943.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)實(shí)體類(lèi)與字符串互相轉(zhuǎn)換的方法
- 01-10時(shí)間戳與時(shí)間相互轉(zhuǎn)換(php .net精確到毫秒)
- 01-10C#實(shí)現(xiàn)HTML轉(zhuǎn)WORD及WORD轉(zhuǎn)PDF的方法
- 01-10C#編程實(shí)現(xiàn)對(duì)象與JSON串互相轉(zhuǎn)換實(shí)例分析
- 01-10Silverlight將圖片轉(zhuǎn)換為byte的實(shí)現(xiàn)代碼
- 01-10C#使用正則表達(dá)式實(shí)現(xiàn)首字母轉(zhuǎn)大寫(xiě)的方法
- 01-10C#中使用JSON.NET實(shí)現(xiàn)JSON、XML相互轉(zhuǎn)換
- 01-10C#簡(jiǎn)易圖片格式轉(zhuǎn)換器實(shí)現(xiàn)方法
- 01-10C#實(shí)現(xiàn)XSL轉(zhuǎn)換的方法
- 01-10C#實(shí)現(xiàn)將文件轉(zhuǎn)換為XML的方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10delphi制作wav文件的方法