C# 汉字转拼音NPinyin
网上找了好多方法,都比较慢。NPinyin经过测试,挺快。
public static class PinYinHelper { /// <summary> /// 汉字转化为拼音 /// </summary> /// <param name="str">汉字</param> /// <returns>全拼</returns> public static string GetPinyin(string str) { var result = ""; if (!string.IsNullOrEmpty(str)) { result = NPinyin.Pinyin.GetPinyin(str)?.Replace(" ", "").ToLower(); } return result; } /// <summary> /// 汉字转化为拼音首字母 /// </summary> /// <param name="str">汉字</param> /// <returns>首字母</returns> public static string GetInitials(string str) { var result = ""; if (!string.IsNullOrEmpty(str)) { result = NPinyin.Pinyin.GetInitials(str)?.Trim().ToLower(); } return result; } }