C# 应用微软的Visual Studio International Pack 类库提取汉字拼音首字母
昨天经过网友提示,提取汉字拼音的办法可以应用微软的一个类库 Visual Studio International Pack ,今天试了一试,确切好用!下面分享下应用办法:
起首下载Visual Studio International Pack 1.0,官方下载地址:http://www.microsoft.com/downloads/zh-cn/details.aspx?FamilyID=44CAC7F0-633B-477D-AED2-99AEE642FC10&displaylang=zh-cn。下载完毕后解压,解压后可以发明7个MSI安装文件,此中CHSPinYinConv.msi是汉字拼音组件,CHTCHSConv.msi是进行繁简体互转组件,安装这两个MSI就可以了 (x86操纵体系上的默认安装目次是 C:\Program Files\Microsoft Visual Studio International Pack\) 。安装完毕后,须要在VS里添加引用,分别引用:C:\Program Files\Microsoft Visual Studio International Pack\Simplified Chinese Pin-Yin Conversion Library(拼音)下和C:\Program Files\Microsoft Visual Studio International Pack\Traditional Chinese to Simplified Chinese Conversion Library and Add-In Tool(繁简互转)下的dll 即可应用。
完成上方的工作后,应用办法就很是简单了,下面看代码:
using Microsoft.International.Converters.PinYinConverter;//导入拼音相干 namespace WebApplication2 { public class Class1 { /// <summary> /// 汉字转化为拼音 /// </summary> /// <param name="str">汉字</param> /// <returns>全拼</returns> public static string GetPinyin(string str) { string r = string.Empty; foreach (char obj in str) { try { ChineseChar chineseChar = new ChineseChar(obj); string t = chineseChar.Pinyins[0].ToString(); r += t.Substring(0, t.Length - 1); } catch { r += obj.ToString(); } } return r; } /// <summary> /// 汉字转化为拼音首字母 /// </summary> /// <param name="str">汉字</param> /// <returns>首字母</returns> public static string GetFirstPinyin(string str) { string r = string.Empty; foreach (char obj in str) { try { ChineseChar chineseChar = new ChineseChar(obj); string t = chineseChar.Pinyins[0].ToString(); r += t.Substring(0, 1); } catch { r += obj.ToString(); } } return r; } } }
调用办法:(重视先引用)
GetPinyin("风影");//获取全拼 GetFirstPinyin("风影");//获取首字母
是不是很是简单呢?有了这个类库就省事多了!趁便再补充一下繁简体互转的办法,某些时辰可能会用到:
先导入
using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter; /// <summary> /// 简体转换为繁体 /// </summary> /// <param name="str">简体字</param> /// <returns>繁体字</returns> public static string GetTraditional(string str) { string r = string.Empty; r = ChineseConverter.Convert(str, ChineseConversionDirection.SimplifiedToTraditional); return r; } /// <summary> /// 繁体转换为简体 /// </summary> /// <param name="str">繁体字</param> /// <returns>简体字</returns> public static string GetSimplified(string str) { string r = string.Empty; r = ChineseConverter.Convert(str, ChineseConversionDirection.TraditionalToSimplified); return r; }
该类库的功能概述
Microsoft Visual Studio International Pack 1.0版 包含以下功能:
|
转载自http://www.cnblogs.com/yazdao/archive/2011/06/04/2072488.html