C# 汉语生成拼音,微软给了一个类库,使用 ChineseChar 类可以轻松获取拼音
1.第一步,需要下载 vsintlpack1.zip 文件
下载地址: http://www.microsoft.com/downloads/details.aspx?FamilyID=44cac7f0-633b-477d-aed2-99aee642fc10&DisplayLang=zh-cn
2.解压vsintlpack1.zip 后,得到CHSPinYinConv.msi,安装CHSPinYinConv.msi,在目录 C:\Program Files (x86)\Microsoft Visual Studio International Pack\Simplified Chinese Pin-Yin Conversion Library 中获取 ChnCharInfo.dll ,复制添加引用即可.
3.写一个方法即可获取全拼
/// <summary>
/// 获取全拼
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static string FullPingyin(string name)
{
string str = "";
foreach (char item in name.ToCharArray())
{
if (Microsoft.International.Converters.PinYinConverter.ChineseChar.IsValidChar(item))
{
Microsoft.International.Converters.PinYinConverter.ChineseChar node = new Microsoft.International.Converters.PinYinConverter.ChineseChar(item);
str += node.Pinyins[0].Substring(0, node.Pinyins[0].Length - 1);
}
else
{
str += item.ToString();
}
}
return str;
}