这是微软官方提供的解决方案,支持简体中午,繁体中文,韩语和日语。
需要安装依赖的 Visual Studio International Pack
下载地址:https://www.microsoft.com/zh-CN/download/details.aspx?id=15251
using System;
using System.Data;
using System.Configuration;
using Microsoft.International.Converters.PinYinConverter;
namespace Utility.Convert
{
/// <summary>
///ChineseConvert 的摘要说明
/// </summary>
public class ChineseConvert
{
public ChineseConvert() { }
/// <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;
}
}
}