当前位置: 移动技术网 > IT编程>开发语言>c# > C# 汉字转拼音

C# 汉字转拼音

2019年08月05日  | 移动技术网IT编程  | 我要评论
安装相关依赖: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];
        }
    }

 

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网