C#汉字转拼音(Microsoft.PinYinConverter)
1. NuGet程序包
Microsoft.PinYinConverter
2. 使用方法
var chineseChar = new ChineseChar('中');
var pyStr = chineseChar.Pinyins
多音字返回多个读音,最后一位代表声调,每次只能转换一个字符
3. 封装
public static class PinYinHelper
{
///<summary>
/// 汉字
/// </summary>
private static string ChineseReg = "^[\\u4E00-\\u9FA5]+$";
public static string GetPinYinFull(string str)
{
var pySb = new StringBuilder();
foreach (var itemChar in str)
{
//过滤非汉字的字符,直接返回
var reg = new Regex(ChineseReg);
if (!reg.IsMatch(itemChar.ToString()))
{
pySb.Append(itemChar);
}
else
{
var chineseChar = new ChineseChar(itemChar);
var pyStr = chineseChar.Pinyins.First().ToLower();
pySb.Append(pyStr.Substring(0, pyStr.Length - 1));
}
}
return pySb.ToString();
}
}