.net core 使用PinYinConverterCore 汉字转拼音
1.全拼音
public static string ToPinyin(this string msg, bool islower=true) { string result = string.Empty; msg= Regex.Replace(msg, @"[^\u4e00-\u9fa5]", string.Empty).Trim(); foreach (char item in msg) { var cc = new ChineseChar(item); if (cc.Pinyins.Count > 0 && cc.Pinyins[0].Length > 0) { string temp = cc.Pinyins[0].ToString(); if (islower) { temp = temp.ToLower(); } result += temp.Substring(0, temp.Length - 1); } } return result; }
2.首字母拼音
public static string ToFirstPinyin(this string msg, bool islower = true) { string result = string.Empty; msg = Regex.Replace(msg, @"[^\u4e00-\u9fa5]", string.Empty).Trim(); foreach (var item in msg) { var cc = new ChineseChar(item); if (cc.Pinyins.Count > 0 && cc.Pinyins[0].Length > 0) { string temp = cc.Pinyins[0].ToString(); if (islower) { temp = temp.ToLower(); } result += temp.Substring(0, 1); } } return result; }