原文:https://blog.csdn.net/younghaiqing/article/details/62417269編碼
static void Main(string[] args) { Console.WriteLine(GetSpellCode("asdf牛逼你水電費")) ; Console.ReadKey(); } /// <summary> /// 在指定的字符串列表CnStr中檢索符合拼音索引字符串 /// </summary> /// <param name="CnStr">漢字字符串</param> /// <returns>相對應的漢語拼音首字母串</returns> public static string GetSpellCode(string CnStr) { string strTemp = ""; int iLen = CnStr.Length; int i = 0; for (i = 0; i <= iLen - 1; i++) { strTemp += GetCharSpellCode(CnStr.Substring(i, 1)); } return strTemp; } /// <summary> /// 獲得一個漢字的拼音第一個字母,若是是一個英文字母則直接返回大寫字母 /// </summary> /// <param name="CnChar">單個漢字</param> /// <returns>單個大寫字母</returns> private static string GetCharSpellCode(string CnChar) { long iCnChar; byte[] ZW = System.Text.Encoding.Default.GetBytes(CnChar); //若是是字母,則直接返回 if (ZW.Length == 1) { return CnChar.ToUpper(); } else { // get the array of byte from the single char int i1 = (short)(ZW[0]); int i2 = (short)(ZW[1]); iCnChar = i1 * 256 + i2; } // iCnChar match the constant if ((iCnChar >= 45217) && (iCnChar <= 45252)) { return "A"; } else if ((iCnChar >= 45253) && (iCnChar <= 45760)) { return "B"; } else if ((iCnChar >= 45761) && (iCnChar <= 46317)) { return "C"; } else if ((iCnChar >= 46318) && (iCnChar <= 46825)) { return "D"; } else if ((iCnChar >= 46826) && (iCnChar <= 47009)) { return "E"; } else if ((iCnChar >= 47010) && (iCnChar <= 47296)) { return "F"; } else if ((iCnChar >= 47297) && (iCnChar <= 47613)) { return "G"; } else if ((iCnChar >= 47614) && (iCnChar <= 48118)) { return "H"; } else if ((iCnChar >= 48119) && (iCnChar <= 49061)) { return "J"; } else if ((iCnChar >= 49062) && (iCnChar <= 49323)) { return "K"; } else if ((iCnChar >= 49324) && (iCnChar <= 49895)) { return "L"; } else if ((iCnChar >= 49896) && (iCnChar <= 50370)) { return "M"; } else if ((iCnChar >= 50371) && (iCnChar <= 50613)) { return "N"; } else if ((iCnChar >= 50614) && (iCnChar <= 50621)) { return "O"; } else if ((iCnChar >= 50622) && (iCnChar <= 50905)) { return "P"; } else if ((iCnChar >= 50906) && (iCnChar <= 51386)) { return "Q"; } else if ((iCnChar >= 51387) && (iCnChar <= 51445)) { return "R"; } else if ((iCnChar >= 51446) && (iCnChar <= 52217)) { return "S"; } else if ((iCnChar >= 52218) && (iCnChar <= 52697)) { return "T"; } else if ((iCnChar >= 52698) && (iCnChar <= 52979)) { return "W"; } else if ((iCnChar >= 52980) && (iCnChar <= 53640)) { return "X"; } else if ((iCnChar >= 53689) && (iCnChar <= 54480)) { return "Y"; } else if ((iCnChar >= 54481) && (iCnChar <= 55289)) { return "Z"; } else return ("?"); }
static void Main(string[] args) { string[] maxims = new string[]{ "事常與人違,事總在人爲", "駿馬是跑出來的,強兵是打出來的", "駕馭命運的舵是奮鬥。不抱有一絲幻想,不放棄一點機會,不中止一日努力。 ", "若是害怕前面跌宕的山岩,生命就永遠只能是死水一潭", "懦弱的人只會裹足不前,莽撞的人只能引爲燒身,只有真正勇敢的人才能所向披靡" }; string[] medicines = new string[] { "聚維酮碘溶液", "開塞露", "爐甘石洗劑", "苯扎氯銨貼", "魚石脂軟膏", "莫匹羅星軟膏", "紅黴素軟膏", "氫化可的鬆軟膏", "曲安奈德軟膏", "丁苯羥酸乳膏", "雙氯芬酸二乙胺乳膏", "凍瘡膏", "克黴唑軟膏", "特比奈芬軟膏", "酞丁安軟膏", "咪康唑軟膏、栓劑", "甲硝唑栓", "複方莪術油栓" }; Console.WriteLine("UTF8句子拼音:"); foreach (string s in maxims) { Console.WriteLine("漢字:{0}\n拼音:{1}\n", s, Pinyin.GetPinyin(s)); } Encoding gb2312 = Encoding.GetEncoding("GB2312"); Console.WriteLine("GB2312拼音簡碼:"); foreach (string m in medicines) { string s = Pinyin.ConvertEncoding(m, Encoding.UTF8, gb2312); Console.WriteLine("藥品:{0}\n簡碼:{1}\n", s, Pinyin.GetInitials(s, gb2312)); } Console.ReadKey(); }