c# 中文轉(zhuǎn)拼音without CJK
Xamarin寫Android程序時,通常要使用按中文首字母分組顯示(如通訊錄) 。
于是需要被迫包含CJK,不過包含后包肯定是會變大的,于是。。。。自己寫了一個硬枚舉的中文轉(zhuǎn)拼音的類。
原理是這樣的:
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
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持我們!
欄 目:C#教程
下一篇:分享兩種實現(xiàn)Winform程序的多語言支持的多種解決方案
本文標題:c# 中文轉(zhuǎn)拼音without CJK
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5943.html
您可能感興趣的文章
- 01-10C#實現(xiàn)實體類與字符串互相轉(zhuǎn)換的方法
- 01-10時間戳與時間相互轉(zhuǎn)換(php .net精確到毫秒)
- 01-10C#實現(xiàn)HTML轉(zhuǎn)WORD及WORD轉(zhuǎn)PDF的方法
- 01-10C#編程實現(xiàn)對象與JSON串互相轉(zhuǎn)換實例分析
- 01-10Silverlight將圖片轉(zhuǎn)換為byte的實現(xiàn)代碼
- 01-10C#使用正則表達式實現(xiàn)首字母轉(zhuǎn)大寫的方法
- 01-10C#中使用JSON.NET實現(xiàn)JSON、XML相互轉(zhuǎn)換
- 01-10C#簡易圖片格式轉(zhuǎn)換器實現(xiàn)方法
- 01-10C#實現(xiàn)XSL轉(zhuǎn)換的方法
- 01-10C#實現(xiàn)將文件轉(zhuǎn)換為XML的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當前工程中所有窗體并
- 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)控當前操作系統(tǒng)已
隨機閱讀
- 01-10C#中split用法實例總結(jié)
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10delphi制作wav文件的方法


