日期:2014-05-17  浏览次数:21200 次

C#获取汉字拼音(包含音调)
//首字母拼音
public static string GetPYString(string str)
         {
            string tempStr = "";
            foreach (char c in str)
            {
                if ((int)c >= 33 && (int)c <= 126)
                {//字母和符号原样保留
                    tempStr += c.ToString();
                }
                else
                {//累加拼音声母
                    tempStr += GetPYChar(c.ToString());
                }
            }
            return tempStr.ToUpper();
        }

        public static string GetPYChar(string c)
        {
            byte[] array = new byte[2];
            if (c.Trim().Length <= 0)
            {
                return " ";
            }
            array = System.Text.Encoding.Default.GetBytes(c);
            if (c.Trim().Length <= 0)
            {
                return "";
            }
            int i = (short)(array[0] - '\0') * 256 + ((short)(array[1] - '\0'));
            if (i < 0xB0A1) return "*";
            if (i < 0xB0C5) return "a";
            if (i < 0xB2C1) return "b";
            if (i < 0xB4EE) return "c";
            if (i < 0xB6EA) return "d";
            if (i < 0xB7A2) return "e";
            if (i < 0xB8C1) return "f";
            if (i < 0xB9FE) return "g";
            if (i < 0xBBF7) return "h";
            if (i < 0xBFA6) return "j";
            if (i < 0xC0AC) return "k";
            if (i < 0xC2E8) return "l";
            if (i < 0xC4C3) return "m";
            if (i < 0xC5B6) return "n";
            if (i < 0xC5BE) return "o";
            if (i < 0xC6DA) return "p";
            if (i < 0xC8BB) return "q";
            if (i < 0xC8F6) return "r";
            if (i < 0xCBFA) return "s";
            if (i < 0xCDDA) return "t";
            if (i < 0xCEF4) return "w";
            if (i < 0xD1B9) return "x";
            if (i < 0xD4D1) return "y";
            if (i < 0xD7FA) return "z";
            return "*";
        }

        /// <summary>
        /// 获取全拼
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetPYAll(string str)
        {
            StringBuilder py = new StringBuilder();
            foreach (char obj in str)
            {
                try
                {
                    ChineseChar chineseChar = new ChineseChar(obj);
                    ReadOnlyCollection<string> pyColl = chineseChar.Pinyins;
                    //循环生成首字母的笛卡尔积,存储到临时拼音列表
                    foreach (var item in pyColl)
                    {
                        if (item != null)
                        {
                            py.Append(item.Remove(item.Length - 1, 1));
                            break;
                        }
                    }
                }
                catch//非汉字
                {
                    py.Append(obj);
                }
            }

            return py.ToString();
        }

///每个汉子的音节都有

/// 汉字转化为拼音,Dictionary的key保存全拼,value存拼音首字母缩写[包含了多音字,因此显示名存在重复行]         /// </summary>         /// <param name="str">汉字</param>         /// <returns>Dictionary的key保存全拼,value存拼音首字母缩写</returns>         public Dictionary<string, string> GetPinyin(string str)         {          &