其他分享
首页 > 其他分享> > 汉字转拼音

汉字转拼音

作者:互联网

程序集 ChnCharInfo.dll 

 public static string ConvertToPinYin(string str)
        {
            string PYstr = "";
            try
            {
                foreach (char item in str.ToCharArray())
                {
                    if (Microsoft.International.Converters.PinYinConverter.ChineseChar.IsValidChar(item))
                    {
                        Microsoft.International.Converters.PinYinConverter.ChineseChar cc = new Microsoft.International.Converters.PinYinConverter.ChineseChar(item);

                        //PYstr += string.Join("", cc.Pinyins.ToArray());
                        PYstr += cc.Pinyins[0].Substring(0, cc.Pinyins[0].Length - 1);
                        //PYstr += cc.Pinyins[0].Substring(0, cc.Pinyins[0].Length - 1).Substring(0, 1).ToLower();
                    }
                    else
                    {
                        PYstr += item.ToString();
                    }
                }
            }
            catch (Exception)
            {
            }
           
            return PYstr;
        }





 /// <summary>
        /// 汉字转拼音 首字母大写 加空格
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ConvertToPascalCasePinYin(string str)
        {
            string stmp = string.Join(" ", System.Text.RegularExpressions.Regex.Split(str.Trim(), "", 
                System.Text.RegularExpressions.RegexOptions.IgnoreCase)).Trim();

            stmp = ConvertToPinYin(stmp).ToLower();

            return stmp;
        }

 

标签:拼音,cc,stmp,汉字,item,PYstr,Pinyins,string
来源: https://blog.csdn.net/qq_21790383/article/details/90056353