zoukankan      html  css  js  c++  java
  • C#拼音帮助类

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

          using Microsoft.International.Converters.PinYinConverter;
          using NPinyin;

    可以在NuGet里面下载

     

    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

     

  • 相关阅读:
    AutoCAD.Net/C#.Net QQ群:193522571 sld文件格式的研究
    AutoCAD.Net/C#.Net QQ群:193522571 程序中需要判断是attdef和text时应该把attdef放在前面
    AutoCAD.Net/C#.Net QQ群:193522571 C#判断文件夹是否已经打开
    AutoCAD.Net/C#.Net QQ群:193522571 treeview中默认选择某一个节点
    AutoCAD.Net/C#.Net QQ群:193522571 WINFORM界面上控件的排版问题
    AutoCAD.Net/C#.Net QQ群:193522571 窗体不闪烁
    远程连接MySQL失败
    Linux后台执行任务且不打印输出到终端
    Linux升级python至3.x
    Linux运行python文件
  • 原文地址:https://www.cnblogs.com/loushengjie/p/10929311.html
Copyright © 2011-2022 走看看