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

C# 汉字转拼音

程序员文章站 2023-11-11 08:31:10
安装相关依赖:NPinyin Microsoft.International.Converters.PinYinConverter 直接从vs里面的nuget管理器搜索下载即可。 ......

安装相关依赖:npinyin

         microsoft.international.converters.pinyinconverter

直接从vs里面的nuget管理器搜索下载即可。

    public class pinyinhelper
    {
        private static encoding gb2312 = encoding.getencoding("gb2312");

        /// <summary>
        /// 汉字转全拼
        /// </summary>
        public static string converttoallspell(string strchinese, idictionary<char, string> pinyindic = null)
        {
            try
            {
                if (strchinese.length != 0)
                {
                    var fullspell = new stringbuilder();
                    for (var i = 0; i < strchinese.length; i++)
                    {
                        var chr = strchinese[i];
                        var pinyin = getspell(chr);
                        fullspell.append(pinyin);
                    }

                    return fullspell.tostring().tolower();
                }
            }
            catch (exception e)
            {
                console.writeline("全拼转化出错!" + e.message);
            }

            return string.empty;
        }

        /// <summary>
        /// 汉字转首字母
        /// </summary>
        /// <param name="strchinese"></param>
        /// <returns></returns>
        public static string getfirstspell(string strchinese)
        {
            //npinyin.pinyin.getinitials(strchinese)  有bug  洺无法识别
            //return npinyin.pinyin.getinitials(strchinese);

            try
            {
                if (strchinese.length != 0)
                {
                    var fullspell = new stringbuilder();
                    for (var i = 0; i < strchinese.length; i++)
                    {
                        var chr = strchinese[i];
                        fullspell.append(getspell(chr)[0]);
                    }

                    return fullspell.tostring().tolower();
                }
            }
            catch (exception e)
            {
                console.writeline("首字母转化出错!" + e.message);
            }

            return string.empty;
        }

        private static string getspell(char chr)
        {
            var coverchr = npinyin.pinyin.getpinyin(chr);
            var ischineses = chinesechar.isvalidchar(coverchr[0]);
            if (ischineses)
            {
                var chinesechar = new chinesechar(coverchr[0]);
                foreach (var value in chinesechar.pinyins)
                {
                    if (!string.isnullorempty(value))
                    {
                        return value.remove(value.length - 1, 1);
                    }
                }
            }

            return coverchr;

        }

        /// <summary>
        /// 从字典获取拼音
        /// </summary>
        /// <param name="c">字</param>
        /// <param name="pinyindic">字典</param>
        /// <returns></returns>
        private static string getfrompinyindic(char c, idictionary<char, string> pinyindic)
        {
            if (pinyindic == null
                || pinyindic.count == 0
                || !pinyindic.containskey(c))
            {
                return "";
            }

            return pinyindic[c];
        }
    }