一一一byhuang

关于汉字转拼音

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

所以自己用微软提供的类库,做了个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>

运行效果:

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



posted on 2011-10-28 16:53  一一一  阅读(360)  评论(5)    收藏  举报

导航