微软拼音转换工具类ChnCharInfo.dll

本文介绍了一种利用微软提供的DLL组件实现汉字到拼音转换的方法,该组件支持获取汉字的拼音、多音字、同音字及笔画数等属性,并提供了一个简单的使用示例,适用于Xamarin.iOS等跨平台移动开发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.简介   

    之前做汉字转拼音是使用各种枚举的方式,将各种情况列举,这种方式出错的机率很大,经常对不上号。(如果你想了解更多:http://www.cnblogs.com/islands/articles/1231613.html

    后来在度娘的过程中,发现了其实微软早有汉字转拼音的组件dll库,里面估计是封装了windows系统下输入法的汉字精华,所以转化过程中相对要准确的多。而我在做Xamarin.iOS的过程中,由于要对联系人进行首字母索引,因此使用到了它。

    官网了解:http://www.microsoft.com/zh-cn/download/confirmation.aspx?id=15251

    使用案例:http://blog.163.com/kunkun0921@126/blog/static/169204332201210735848402/

    优快云下载:http://download.youkuaiyun.com/detail/newxdlysk/4293850

2.主要功能

   1)支持获取简体中文字符的常用属性:拼音,多音字,同音字,笔画数

   2)繁简中文互相转化

   注:虽然很方便,但检索过程中的时间消耗还是挺长的;还有就是一些存在翻译争议的地区名称会跟目前我们拼音有所出入:如广州(Canton/Guangzhuo).

3.简单使用(首字母的获取)   

 

public string Convert (string chr)
        {
            try{
                if (chr.Length != 0) {
                    StringBuilder fullSpell = new StringBuilder ();
                    for (int i=0; i<chr.Length; i++) {
                        bool isChineses = ChineseChar.IsValidChar (chr [i]);
                        if (isChineses) {
                            ChineseChar chineseChar = new ChineseChar (chr [i]);
                            foreach (string value in chineseChar.Pinyins) {
                                if (!string.IsNullOrEmpty (value)) {
                                    fullSpell.Append (value.Remove(value.Length - 1, 1));
                                    break;
                                }
                            }
                        }else {
                            fullSpell.Append(chr[i]);
                        }
                    }
 
                    return fullSpell.ToString().ToUpper();
                } 
            }catch(Exception e){
                Console.WriteLine("全拼转化出错!"+e.Message);
            }
 
            return string.Empty;
        }
 
        /// <summary>
        /// 获取首字母
        /// </summary>
        public string SubFirstLetter(string chr)
        {
            try{
                chr = chr.Substring(0,1);
                if (chr.Length != 0) {
                    StringBuilder fullSpell = new StringBuilder ();
                    for (int i=0; i<chr.Length; i++) {
                        bool isChineses = ChineseChar.IsValidChar (chr [i]);
                        if (isChineses) {
                            ChineseChar chineseChar = new ChineseChar (chr [i]);
                            foreach (string value in chineseChar.Pinyins) {
                                if (!string.IsNullOrEmpty (value)) {
                                    fullSpell.Append (value.Remove(value.Length - 1, 1));
                                    break;
                                }
                            }
                        }else {
                            fullSpell.Append(chr[i]);
                        }
                    }
 
                    return fullSpell.ToString().Substring(0,1).ToUpper();
                } 
            }catch(Exception e){
                Log.Error("首字母转化出错!"+e.Message);
            }
 
            return string.Empty;
        }
    }

 

转载于:https://www.cnblogs.com/webenh/p/9403708.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值