首页 > Asp.net开发 > C#获取汉子拼音(包含音调)

C#获取汉子拼音(包含音调)

//首字母拼音

    1. public static string GetPYString(string str)
    2.          {
    3.             string tempStr = "";
    4.             foreach (char c in str)
    5.             {
    6.                 if ((int)c >= 33 && (int)c <= 126)
    7.                 {//字母和符号原样保留
    8.                     tempStr += c.ToString();
    9.                 }
    10.                 else
    11.                 {//累加拼音声母
    12.                     tempStr += GetPYChar(c.ToString());
    13.                 }
    14.             }
    15.             return tempStr.ToUpper();
    16.         }
    17.         public static string GetPYChar(string c)
    18.         {
    19.             byte[] array = new byte[2];
    20.             if (c.Trim().Length <= 0)
    21.             {
    22.                 return " ";
    23.             }
    24.             array = System.Text.Encoding.Default.GetBytes(c);
    25.             if (c.Trim().Length <= 0)
    26.             {
    27.                 return "";
    28.             }
    29.             int i = (short)(array[0] - '\0') * 256 + ((short)(array[1] - '\0'));
    30.             if (i < 0xB0A1) return "*";
    31.             if (i < 0xB0C5) return "a";
    32.             if (i < 0xB2C1) return "b";
    33.             if (i < 0xB4EE) return "c";
    34.             if (i < 0xB6EA) return "d";
    35.             if (i < 0xB7A2) return "e";
    36.             if (i < 0xB8C1) return "f";
    37.             if (i < 0xB9FE) return "g";
    38.             if (i < 0xBBF7) return "h";
    39.             if (i < 0xBFA6) return "j";
    40.             if (i < 0xC0AC) return "k";
    41.             if (i < 0xC2E8) return "l";
    42.             if (i < 0xC4C3) return "m";
    43.             if (i < 0xC5B6) return "n";
    44.             if (i < 0xC5BE) return "o";
    45.             if (i < 0xC6DA) return "p";
    46.             if (i < 0xC8BB) return "q";
    47.             if (i < 0xC8F6) return "r";
    48.             if (i < 0xCBFA) return "s";
    49.             if (i < 0xCDDA) return "t";
    50.             if (i < 0xCEF4) return "w";
    51.             if (i < 0xD1B9) return "x";
    52.             if (i < 0xD4D1) return "y";
    53.             if (i < 0xD7FA) return "z";
    54.             return "*";
    55.         }
    56.         /// <summary>
    57.         /// 获取全拼
    58.         /// </summary>
    59.         /// <param name="str"></param>
    60.         /// <returns></returns>
    61.         public static string GetPYAll(string str)
    62.         {
    63.             StringBuilder py = new StringBuilder();
    64.             foreach (char obj in str)
    65.             {
    66.                 try
    67.                 {
    68.                     ChineseChar chineseChar = new ChineseChar(obj);
    69.                     ReadOnlyCollection<string> pyColl = chineseChar.Pinyins;
    70.                     //循环生成首字母的笛卡尔积,存储到临时拼音列表
    71.                     foreach (var item in pyColl)
    72.                     {
    73.                         if (item != null)
    74.                         {
    75.                             py.Append(item.Remove(item.Length - 1, 1));
    76.                             break;
    77.                         }
    78.                     }
    79.                 }
    80.                 catch//非汉字
    81.                 {
    82.                     py.Append(obj);
    83.                 }
    84.             }
    85.             return py.ToString();
    86.         }
    [html][/html] view plaincopy

    1. <p>///每个汉子的音节都有</p><p>/// 汉字转化为拼音,Dictionary的key保存全拼,value存拼音首字母缩写[包含了多音字,因此显示名存在重复行]
    2.         /// </summary>
    3.         /// <param name="str">汉字</param>
    4.         /// <returns>Dictionary的key保存全拼,value存拼音首字母缩写</returns>
    5.         public Dictionary<string, string> GetPinyin(string str)
    6.         {
    7.             //str = "其大1a,@";
    8.             Dictionary<string, string> dPinYin = new Dictionary<string, string>();
    9.             Dictionary<string, string> dPinYinTemp = new Dictionary<string, string>();
    10.             string py;
    11.             List<string> lTemp = new List<string>();//临时拼音首字母列表,去除重复字母</p><p>            foreach (char obj in str)
    12.             {
    13.                 try
    14.                 {
    15.                     ChineseChar chineseChar = new ChineseChar(obj);
    16.                     ReadOnlyCollection<string> pyColl = chineseChar.Pinyins;</p><p>                    lTemp.Clear();
    17.                     dPinYinTemp.Clear();</p><p>                    //循环生成首字母的笛卡尔积,存储到临时拼音列表
    18.                     foreach (var item in pyColl)
    19.                     {
    20.                         if (item != null)
    21.                         {
    22.                             //py = IsFirst ? py = item.Substring(0, 1) : item.Remove(item.Length - 1, 1);
    23.                             py = item.Substring(0, 1);</p><p>                            if (!lTemp.Contains(py))
    24.                             {
    25.                                 lTemp.Add(py);
    26.                                 if (dPinYin.Count == 0)
    27.                                 {
    28.                                     dPinYinTemp.Add(item.Remove(item.Length - 1, 1), py);
    29.                                 }
    30.                                 else
    31.                                 {
    32.                                     foreach (var d in dPinYin)
    33.                                     {
    34.                                         dPinYinTemp.Add(d.Key + py, d.Value + py.Substring(0, 1));
    35.                                     }
    36.                                 }
    37.                             }
    38.                         }
    39.                     }
    40.                 }
    41.                 catch//非汉字
    42.                 {
    43.                     if (dPinYin.Count == 0)
    44.                     {
    45.                         dPinYinTemp.Add(obj.ToString(), obj.ToString());
    46.                     }
    47.                     else
    48.                     {
    49.                         dPinYinTemp.Clear();
    50.                         foreach (var d in dPinYin)
    51.                         {
    52.                             dPinYinTemp.Add(d.Key + obj.ToString(), d.Value + obj.ToString());
    53.                         }
    54.                     }
    55.                 }
    56.                 //拷贝到返回列表
    57.                 dPinYin.Clear();
    58.                 dPinYin = new Dictionary<string, string>(dPinYinTemp);
    59.             }</p><p>            return dPinYin;
    60.         }</p>

本文固定链接: http://www.devba.com/index.php/archives/2461.html | 开发吧

报歉!评论已关闭.