欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

根据汉字获取它的字符串拼音首字母(大写),含多音字

程序员文章站 2023-02-28 14:06:49
  ///         /// 根据汉字获取它的字符串拼音首字...

  /// <summary>
        /// 根据汉字获取它的字符串拼音首字母(大写),含多音字
        /// </summary>
        /// <param name="chineseStr"></param>
        /// <returns></returns>
        public static string GetFirstSpellByChinese(string chineseStr)
        {
            int i, j, k, m;
            string tmpStr;
            string returnStr = "";  //返回最终结果的字符串
            string[] tmpArr;
            for (i = 0; i < chineseStr.Length; i++)
            {   //处理汉字字符串,对每个汉字的首字母进行一次循环
                tmpStr = GetShellByChinese((char)chineseStr[i]);   //获取第i个汉字的拼音首字母,可能为1个或多个
                if (tmpStr.Length > 0)
                {   //汉字的拼音首字母存在的情况才进行操作
                    if (returnStr != "")
                    {   //不是第一个汉字
                        Regex regex = new Regex(",");
                        tmpArr = regex.Split(returnStr);
                        returnStr = "";
                        for (k = 0; k < tmpArr.Length; k++)
                        {
                            for (j = 0; j < tmpStr.Length; j++)    //对返回的每个首字母进行拼接
                            {
                                string charcode = tmpStr[j].ToString(); //取出第j个拼音字母
                                returnStr += tmpArr[k] + charcode + ",";
                            }
                        }
                        if (returnStr != "")
                            returnStr = returnStr.Substring(0, returnStr.Length - 1);
                    }
                    else
                    {   //构造第一个汉字返回结果
                        for (m = 0; m < tmpStr.Length - 1; m++)
                            returnStr += tmpStr[m] + ",";
                        returnStr += tmpStr[tmpStr.Length - 1];
                    }
                }
            }
            return returnStr;   //返回处理结果字符串,以,分隔每个拼音组合
        }
        /// <summary>
        /// 获取单个汉字对应的拼音首字符字符串
        /// </summary>
        /// <param name="chineseCh"></param>
        /// <returns></returns>
        private static string GetShellByChinese(char chineseCh)
        {
            // 汉字拼音首字母列表 本列表包含了20902个汉字,收录的字符的Unicode编码范围为19968至40869
            string strChineseFirstPY = 
            //此处收录了375个多音字
            string MultiPinyin = 
            string resStr = "";
            int i, j, uni;
            uni = (UInt16)chineseCh;
            if (uni > 40869 || uni < 19968)
                return resStr;
            //返回该字符在Unicode字符集中的编码值
            i = MultiPinyin.IndexOf(uni.ToString());
            //检查是否是多音字,是按多音字处理,不是就直接在strChineseFirstPY字符串中找对应的首字母
            if (i < 0)
            //获取非多音字汉字首字母
            {
                resStr = strChineseFirstPY[uni - 19968].ToString();
            }
            else
            {   //获取多音字汉字首字母
                j = MultiPinyin.IndexOf(",", i);
                resStr = MultiPinyin.Substring(i + 6, j - i - 6);
            }

               return resStr;
        }