C#拼音帮助类

 如果使用此帮助类需要引用

  可以在NuGet里面下载

      Microsoft.International.Converters.PinYinConverter;
      NPinyin;

可以在

 

   链接:https://pan.baidu.com/s/1dBtaP_3jbelfQYYizBg_Vg 

   提取码:5yw0

下载获取这两个dll文件

 

 

1.编码格式为GB2312

      /// <summary>
        /// lou  2019年5月27日10:17:48 Encoding编码
        /// </summary>
        private static readonly Encoding Gb2312 = Encoding.GetEncoding("GB2312");

 

2.汉字转全拼

 /// <summary>
        ///lou 2019年5月27日10:25:00 汉字转全拼
        /// </summary>
        /// <param name="strChinese">汉字</param>
        /// <returns></returns>
        public static string ConvertToAllSpell(string strChinese, IDictionary<char, string> pinyinDic = null)
        {
            try
            {
                if (strChinese.Length != 0)
                {
                    StringBuilder fullSpell = new StringBuilder();
                    for (int i = 0; i < strChinese.Length; i++)
                    {
                        var chr = strChinese[i];
                        string pinyin = string.Empty;
                        if (pinyin.Length == 0)
                        {
                            pinyin = GetSpell(chr);
                        }
                        fullSpell.Append(pinyin);
                    }

                    return fullSpell.ToString().ToLower();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("全拼转化出错!" + e.Message);
            }

            return string.Empty;
        }
View Code

 

3.汉字转首字母

 /// <summary>
        /// lou 2019年5月27日10:19:57 汉字转首字母
        /// </summary>
        /// <param name="strChinese">汉字</param>
        /// <returns></returns>
        public static string GetFirstSpell(string strChinese)
        {
            try
            {
                if (strChinese.Length != 0)
                {
                    StringBuilder fullSpell = new StringBuilder();
                    for (int i = 0; i < strChinese.Length; i++)
                    {
                        var chr = strChinese[i];
                        fullSpell.Append(GetSpell(chr)[0]);
                    }

                    return fullSpell.ToString().ToUpper();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("首字母转化出错!" + e.Message);
            }

            return string.Empty;
        }
View Code

 

4.获取字符拼音

/// <summary>
        /// lou 2019年5月27日10:20:22 获取字符拼音
        /// </summary>
        /// <param name="chr">字符</param>
        /// <returns></returns>
        private static string GetSpell(char chr)
        {
            var coverchr = Pinyin.GetPinyin(chr);
            bool isChineses = ChineseChar.IsValidChar(coverchr[0]);
            if (isChineses)
            {
                ChineseChar chineseChar = new ChineseChar(coverchr[0]);
                foreach (string value in chineseChar.Pinyins)
                {
                    if (!string.IsNullOrEmpty(value))
                    {
                        return value.Remove(value.Length - 1, 1);
                    }
                }
            }

            return coverchr;
        }
View Code

 

 

posted @ 2019-05-27 10:23  聖潔  阅读(811)  评论(0编辑  收藏  举报