zoukankan      html  css  js  c++  java
  • C# 汉字转拼音

    安装相关依赖: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];
            }
        }
  • 相关阅读:
    xml/xslt常用转义字符
    用ScriptManager实现Web服务的异步调用
    声明静态方法和实例方法的原则
    http request header 中的host行的作用
    获取客户端数据
    HTTP 处理程序(HttpHandlers)
    display与visibility
    会话管理
    ASP.NET 管道
    HttpContext, HttpRequest, HttpResponse
  • 原文地址:https://www.cnblogs.com/dawenyang/p/11304179.html
Copyright © 2011-2022 走看看