private string ConvertToPinYin(string input) { StringBuilder sb = new StringBuilder(); foreach (var item in input) { string s= new ChineseChar(item).Pinyins[0]; sb.Append(s.Substring(0,s.Length-1)); } return sb.ToString(); }
/// <summary> /// 拼音转换 /// </summary> /// <param name="str"></param> /// <param name="simple"></param> /// <returns></returns> public static string ToSpell(this string str, bool simple = false) { if (string.IsNullOrEmpty(str)) return ""; string fullPath = AppConfig.BasePath + AppConfig.GetConfig("pinypath"); Dictionary<string, string[]> dict = FileHelper.ReadFileSplit(fullPath, "|"); if (dict != null && dict.Count > 0) { foreach (string key in dict.Keys) { string[] value = dict[key]; if (str.Contains(key)) { if (simple) { str = value.Length > 1 ? str.Replace(key, value[1]) : str.Replace(key, value[0].Substring(0, 1)); } else { str = str.Replace(key, value[0]); } } } } List<string> res = new List<string>(); foreach (char c in str) { if (ChineseChar.IsValidChar(c)) { string piny = new ChineseChar(c).Pinyins[0]; piny = piny.Substring(0, piny.Length - 1); res.Add(piny.ToLower()); } else { res.Add(c.ToString()); } } return simple ? string.Join("", res.Select(r => r.Substring(0, 1))) : string.Join("", res); }