码迷,mamicode.com
首页 > Web开发 > 详细

.net汉字转字母

时间:2017-01-06 12:05:59      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:链接   pac   首字母   als   end   ace   拼音   text   密码   

目前手上有一个需要实现:将用户输入的姓名转换成汉语拼音。

使用枚举,既麻烦又易出错,发现有一个微软拼音转换工具类ChnCharInfo.dll,在此记录下:

首先需要引入此dll,

链接: http://pan.baidu.com/s/1hsa9Y40 密码: ijdi

写一个转换的Helper类:

public class ChineseToCharHelper
    {
        /// <summary>得到汉字拼音(Item1:全拼 2:首字母缩写 3:大写开头全拼)</summary>
        /// <param name="chinese"></param>
        /// <returns></returns>
        public static Tuple<string, string, string> PinYinString(string chinese)
        {
            if (string.IsNullOrWhiteSpace(chinese)) return new Tuple<string, string, string>(string.Empty, string.Empty, string.Empty);
            char[] ch = chinese.ToArray();
            string allPinYin = string.Empty, abbPinYin = string.Empty, firstAllPinYin = string.Empty;
            foreach (char c in ch)
            {
                if (ChineseChar.IsValidChar(c))
                {
                    ChineseChar chineseChar = new ChineseChar(c);
                    ReadOnlyCollection<string> pinyin = chineseChar.Pinyins;
                    var duoyinList = pinyin.Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => x.Length >= 1 ? x.Substring(0, x.Length - 1).ToLower() : string.Empty).ToList();
                    var dic = duoyinList.GroupBy(x => x).ToDictionary(x => x.Key, y => y.Count()).OrderByDescending(x => x.Value);
                    if (dic.Any())
                    {
                        if (dic.First().Value.Equals(1))
                        {
                            abbPinYin += pinyin[0] != null && pinyin[0].Length >= 1
                                ? pinyin[0].Substring(0, 1)
                                : string.Empty;
                            allPinYin += pinyin[0] != null && pinyin[0].Length > 1
                                ? pinyin[0].Substring(0, pinyin[0].Length - 1)
                                : pinyin[0] != null && pinyin[0].Length.Equals(1)
                                    ? pinyin[0].Substring(0, 1)
                                    : string.Empty;
                            var temp = pinyin[0] != null && pinyin[0].Length > 1
                                ? pinyin[0].Substring(0, pinyin[0].Length - 1)
                                : pinyin[0] != null && pinyin[0].Length.Equals(1)
                                    ? pinyin[0].Substring(0, 1)
                                    : string.Empty;
                            firstAllPinYin += System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(temp.ToLower());
                        }
                        else
                        {
                            var value = dic.First().Key;
                            abbPinYin += value.Length >= 1 ? value.Substring(0, 1) : string.Empty;
                            allPinYin += value;
                            firstAllPinYin += System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(value.ToLower());
                        }
                    }
                }
                else
                {
                    abbPinYin += c.ToString();
                    allPinYin += c.ToString();
                    firstAllPinYin += System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(c.ToString().ToLower());
                }
            }
            return new Tuple<string, string, string>(allPinYin.ToLower(), abbPinYin.ToLower(), firstAllPinYin);
        }
    }

直接调用就可以啦。

.net汉字转字母

标签:链接   pac   首字母   als   end   ace   拼音   text   密码   

原文地址:http://www.cnblogs.com/ericli-ericli/p/6255744.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!