若是使用此幫助類須要引用ide
using Microsoft.International.Converters.PinYinConverter;
using NPinyin;ui
能夠在NuGet裏面下載編碼
1.編碼格式爲GB2312spa
/// <summary> /// lou 2019年5月27日10:17:48 Encoding編碼 /// </summary> private static readonly Encoding Gb2312 = Encoding.GetEncoding("GB2312");
2.漢字轉全拼code
/// <summary> ///lou 2019年5月27日10:25:00 漢字轉全拼 /// </summary> /// <param name="strChinese">漢字</param> /// <returns></returns> public static string ConvertToAllSpell(string strChinese, IDictionary<char, string> pinyinDic = null) { try { if (strChinese.Length != 0) { StringBuilder fullSpell = new StringBuilder(); for (int i = 0; i < strChinese.Length; i++) { var chr = strChinese[i]; string pinyin = string.Empty; if (pinyin.Length == 0) { pinyin = GetSpell(chr); } fullSpell.Append(pinyin); } return fullSpell.ToString().ToLower(); } } catch (Exception e) { Console.WriteLine("全拼轉化出錯!" + e.Message); } return string.Empty; }
3.漢字轉首字母blog
/// <summary> /// lou 2019年5月27日10:19:57 漢字轉首字母 /// </summary> /// <param name="strChinese">漢字</param> /// <returns></returns> public static string GetFirstSpell(string strChinese) { try { if (strChinese.Length != 0) { StringBuilder fullSpell = new StringBuilder(); for (int i = 0; i < strChinese.Length; i++) { var chr = strChinese[i]; fullSpell.Append(GetSpell(chr)[0]); } return fullSpell.ToString().ToUpper(); } } catch (Exception e) { Console.WriteLine("首字母轉化出錯!" + e.Message); } return string.Empty; }
4.獲取字符拼音string
/// <summary> /// lou 2019年5月27日10:20:22 獲取字符拼音 /// </summary> /// <param name="chr">字符</param> /// <returns></returns> private static string GetSpell(char chr) { var coverchr = Pinyin.GetPinyin(chr); bool isChineses = ChineseChar.IsValidChar(coverchr[0]); if (isChineses) { ChineseChar chineseChar = new ChineseChar(coverchr[0]); foreach (string value in chineseChar.Pinyins) { if (!string.IsNullOrEmpty(value)) { return value.Remove(value.Length - 1, 1); } } } return coverchr; }