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

     

  • 相关阅读:
    中国移动校园WLAN客户端及使用方法
    Win7 开启upnp服务,直接在网络中设置连接路由设备
    转载:迷你云 – 搭建自己的本地多人团队Dropbox 服务
    JLINK V8固件烧录指导
    实验室网站试运营期间的信息管理
    并非如你想象的那般强大,带你重新认识3D打印
    转:技术宅逆天了!如何从按键音中听出周鸿祎的手机号码
    南大学生破译周鸿祎电话获得互联网大佬青睐
    E430 加装固态硬盘(SSD)参考
    iptables_cacti_nagios
  • 原文地址:https://www.cnblogs.com/loushengjie/p/10929311.html
Copyright © 2011-2022 走看看