private string Hanzi2Pinyin(RequestModel request) { var result = new StringBuilder(); // 解析从客户端来的输出格式设置 PinyinFormat format = PinyinUtil.ParseFormat(request.ToneType) | PinyinUtil.ParseFormat(request.CaseType) | PinyinUtil.ParseFormat(request.VType); foreach (char ch in request.Key) { if (!PinyinUtil.IsHanzi(ch)) {// 不是汉字直接追加 result.Append(ch); continue; } // 是汉字才处理 // 是否只取第一个拼音 if (request.Multi.Equals("first", StringComparison.OrdinalIgnoreCase)) { // 拼音间追加一个空格,这里如果是多间字,拼音可能不准确 result.AppendFormat("{0} ", Pinyin4Net.GetFirstPinyin(ch, format)); continue; } string[] py = Pinyin4Net.GetPinyin(ch, format); result.AppendFormat("({0}) ", string.Join(",", py)); } return(result.ToString()); }
public void FirstOfMultiPinyin() { var pinyin = Pinyin4Net.GetFirstPinyin('传'); Assert.AreEqual("chuan2", pinyin); }
/// <summary> /// 判断拼音格式 /// </summary> /// <param name="hanzi"></param> /// <param name="expected"></param> /// <param name="format"></param> private void PinyinFormatAssert(char hanzi, string expected, PinyinFormat format) { var fmted = Pinyin4Net.GetFirstPinyin(hanzi, format); Assert.AreEqual(expected, fmted); }