C# 拼音通用类
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C# 拼音通用类,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5303字,纯文字阅读大概需要8分钟。
内容图文
因为项目需求,查找资料并编写了这个拼音通用类。在博客园分享:)
其中的多音字首拼,感觉还是非常实用的。可识别中文、英文和符号
1 using System; 2 using System.Collections.Generic; 3 using System.Collections.ObjectModel; 4 using System.Linq; 5 using System.Text; 6 using Microsoft.International.Converters.PinYinConverter; 7 8 namespace MyConsoleApp 9 { 10 public sealed class PinYin 11 { 12 private PinYin() { } 13 14 /// <summary> 15 /// 获取首拼或全拼。大写 16 /// </summary> 17 /// <param name="chineseString"></param> 18 /// <param name="isFull"> true为全拼,false为首拼 </param> 19 /// <returns></returns> 20 public static string GetPY(string chineseString, bool isFull) 21 { 22 StringBuilder sb = new StringBuilder(50); 23foreach (var item in chineseString.ToCharArray()) 24 { 25if (!ChineseChar.IsValidChar(item)) 26 { 27 sb.Append(item); 28 } 29else 30 { 31 System.Collections.ObjectModel.ReadOnlyCollection<string> py = new ChineseChar(item).Pinyins; 32if (py.Count > 0 && py[0].Length > 0) 33 { 34if (isFull) 35 sb.Append(py[0].Substring(0, py[0].Length - 1)); 36else 37 sb.Append(py[0][0]); 38 } 39 } 40 } 41return sb.ToString(); 42 } 43 44///<summary> 45/// 获取首拼和全拼 46///</summary> 47///<param name="chineseString"></param> 48///<param name="pyStr"></param> 49///<param name="pyFullStr"></param> 50publicstaticvoid GetPY(string chineseString, outstring pyStr, outstring pyFullStr) 51 { 52 StringBuilder sb = new StringBuilder(50); 53 StringBuilder sbFull = new StringBuilder(50); 54foreach (var item in chineseString.ToCharArray()) 55 { 56if (!ChineseChar.IsValidChar(item)) 57 { 58 sb.Append(item); 59 sbFull.Append(item); 60 } 61else 62 { 63 System.Collections.ObjectModel.ReadOnlyCollection<string> py = new ChineseChar(item).Pinyins; 64if (py.Count > 0 && py[0].Length > 0) 65 { 66 sbFull.Append(py[0].Substring(0, py[0].Length - 1)); 67 sb.Append(py[0][0]); 68 } 69 } 70 } 71 72 pyStr = sb.ToString(); 73 pyFullStr = sbFull.ToString(); 74 } 75 76///<summary> 77/// 获取首拼(多音字"|"分隔)和全拼 78///</summary> 79///<param name="chineseString"></param> 80///<param name="pyStr"></param> 81///<param name="pyFullStr"></param> 82publicstaticvoid GetHomoPhonePY(string chineseString, outstring pyStr, outstring pyFullStr) 83 { 84 StringBuilder sb = new StringBuilder(50); 85 StringBuilder sbFull = new StringBuilder(50); 86 87char[] chineseCharArray = chineseString.ToCharArray(); 88 Dictionary<int, List<string>> dic = new Dictionary<int, List<string>>(); 89 90for (int i = 0; i < chineseCharArray.Length; i++) 91 { 92char item = chineseCharArray[i]; 93if (!ChineseChar.IsValidChar(item)) 94 { 95 sb.Append(item); 96 sbFull.Append(item); 97 } 98else 99 { 100 ChineseChar chinaChar = new ChineseChar(item); 101 ReadOnlyCollection<string> py = chinaChar.Pinyins; 102if (py.Count > 0 && py[0].Length > 0) 103 { 104 sbFull.Append(py[0].Substring(0, py[0].Length - 1)); 105 dic.Add(i, py.Where(p => !string.IsNullOrEmpty(p)).Select(p => p[0].ToString()).Distinct().ToList()); 106 } 107 } 108 } 109110 OutPutHomoPhone(dic, dic.Keys.Count, 0, "", ref sb); 111112 pyStr = sb.ToString(); 113 pyFullStr = sbFull.ToString(); 114 } 115116///<summary>117/// 获取多音字首拼(多音字"|"分隔) 118/// 西门子SIEMENS商标->XMZSIEMENSSB 119///</summary>120///<param name="chineseString"></param>121///<returns></returns>122publicstaticstring GetHomoPhonePY(string chineseString) 123 { 124 StringBuilder sb = new StringBuilder(50); 125126char[] chineseCharArray = chineseString.ToCharArray(); 127 Dictionary<int, List<string>> dic = new Dictionary<int, List<string>>(); 128129for (int i = 0; i < chineseCharArray.Length; i++) 130 { 131char item = chineseCharArray[i]; 132if (!ChineseChar.IsValidChar(item)) 133 { 134 dic.Add(i, new List<string> { item.ToString() }); 135 } 136else137 { 138 ChineseChar chinaChar = new ChineseChar(item); 139 ReadOnlyCollection<string> py = chinaChar.Pinyins; 140if (py.Count > 0 && py[0].Length > 0) 141 { 142 dic.Add(i, py.Where(p => !string.IsNullOrEmpty(p)).Select(p => p[0].ToString()).Distinct().ToList()); 143 } 144 } 145 } 146147 OutPutHomoPhone(dic, dic.Keys.Count, 0, "", ref sb); 148149return sb.ToString(); 150 } 151152///<summary>153/// 递归出多音字的首拼 154///</summary>155///<param name="dic">多音字字典</param>156///<param name="len">字数</param>157///<param name="key">字典下标</param>158///<param name="parentValue">递归的首拼</param>159///<param name="sb">结果对象</param>160privatestaticvoid OutPutHomoPhone( 161 Dictionary<int, List<string>> dic, 162int len, 163int key, 164string parentValue, 165ref StringBuilder sb) 166 { 167if (dic.Keys.Contains(key)) 168 { 169foreach (string x in dic[key]) 170 { 171if (dic.Keys.Count - 1 >= key) 172 { 173string outString = string.Format("{0}{1}", parentValue, x); 174if (outString.Length == len) 175 { 176 sb.AppendFormat("{0}|", outString); 177 } 178 OutPutHomoPhone(dic, len, key + 1, outString, ref sb); 179 } 180 } 181 } 182 } 183184 } 185 }
原文:http://www.cnblogs.com/daone/p/3844737.html
内容总结
以上是互联网集市为您收集整理的C# 拼音通用类全部内容,希望文章能够帮你解决C# 拼音通用类所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。