关于汉字转拼音

网上找了很多关于汉字转拼音的方法,很多网友提供了不错的方法,但是很多生僻字都转不出拼音或转错。

所以自己用微软提供的类库,做了个DEMO,也随便做下笔记。

下载地址:http://www.microsoft.com/downloads/zh-cn/details.aspx?FamilyID=44cac7f0-633b-477d-aed2-99aee642fc10&DisplayLang=zh-cn

安装完成后可以看到:

在项目上直接引用"ChnCharInfo.dll"即可。

在代码中引用using Microsoft.International.Converters.PinYinConverter命名空间。

 1  protected void btnTransInPinyin_Click(object sender, EventArgs e)
2 {
3 foreach (char c in txtInput.Text.Trim())
4 {
5 ChineseChar chineseChar = new ChineseChar(c);
6 lblShortKey.Text += chineseChar.Pinyins[0].Substring(0, 1).ToLower();//获取汉字拼音首字母
7 lblLongKey.Text += chineseChar.Pinyins[0].Substring(0, chineseChar.Pinyins[0].Length - 1).ToLower();
8 //获取汉字拼音全拼,因为Pinyins这个属性最有一个字符是汉字声调,所以去除
9 }
10 }

复制代码

<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtInput" runat="server"></asp:TextBox>
<asp:Button ID="btnTransInPinyin" runat="server" Text="Button" OnClick="btnTransInPinyin_Click"/></div>
<div>
<asp:Label ID="lblShortKey" runat="server"></asp:Label>
<br />
<asp:Label ID="lblLongKey" runat="server"></asp:Label>
</div>
</form>
</body>

复制代码

运行效果:

测试了几个生僻字。目前还未测试出哪个生僻字是不能转成拼音的。



广告来啦: 我的淘宝店 女装频道 天天疯狂购 聚划算精选频道 天天疯狂购 秒杀满立减 淘宝皇冠店铺精选 女装风向标 鞋包配饰风向标 服饰箱包 天天疯狂购 点击链接支持我的博客 点击链接支持我的博客 点击链接支持我的博客 点击链接支持我的博客
原文地址:https://www.cnblogs.com/suizhikuo/p/2412325.html