• .net汉字转字母


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

    使用枚举,既麻烦又易出错,发现有一个微软拼音转换工具类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);
            }
        }

    直接调用就可以啦。

    但是,组件存在BUG:

    就像图中显示的样子,“汤” ,转换后的拼音是错误的。

    其他的汉字转化吗,到还有发现。毕竟少数,特殊处理下就可以了,组件还是可用的。

  • 相关阅读:
    Java反射(2)访问字段(Field)
    《编程珠玑》中“位图排序”引发的一系列实验
    Java : 传值or传引用?
    const 指针
    三种数据库访问——Spring3.2 + Hibernate4.2
    三种数据库访问——Spring JDBC
    数据库连接池:Druid
    三种数据库访问——原生JDBC
    介绍4款json的java类库 及 其性能测试
    云存储(Swift+Keystone)部署策略
  • 原文地址:https://www.cnblogs.com/ericli-ericli/p/6255744.html
Copyright © 2020-2023  润新知