.Net常用技巧_汉字转拼音类(除汉字外,其余的原样输出)
注:如果是多音字,则默认取第一个拼音。
using System; using System.Collections.Generic; using System.Text; using Microsoft.International.Converters.PinYinConverter; namespace MyTool { /// <summary> /// 汉字转拼音类 /// </summary> public class PinYinConver { public PinYinConver() { // // TODO: 在此处添加构造函数逻辑 // } #region 自定义方法 /// <summary> /// 将字符串转换成首个拼音 /// </summary> /// <param name="chineseStr">字符串</param> /// <returns></returns> private static string ConvertToFirstPinYin(string chineseStr) { if (chineseStr == null) return ""; char[] charArray = chineseStr.ToCharArray(); StringBuilder sb = new StringBuilder(); foreach (char c in charArray) { ChineseChar chineseChar = new ChineseChar(c); System.Collections.ObjectModel.ReadOnlyCollection<string> pyColl = chineseChar.Pinyins; foreach (string py in pyColl) { if (py != null) { sb.Append(py.Substring(0, 1)); break; } } } return sb.ToString(); } /// <summary> /// 将字符串转换成拼音(如遇多音字取第一个读音) /// </summary> /// <param name="chineseStr">汉字字符串</param> /// <returns></returns> private static string ConvertToPinYin(string chineseStr) { if (chineseStr == null) throw new ArgumentNullException("chineseStr"); char[] charArray = chineseStr.ToCharArray(); StringBuilder sb = new StringBuilder(); List<string> listPinyin = new List<string>(); foreach (char c in charArray) { ChineseChar chineseChar = new ChineseChar(c); System.Collections.ObjectModel.ReadOnlyCollection<string> pyColl = chineseChar.Pinyins; if (pyColl[0] == null) continue; string strPinyin = pyColl[0].Substring(0, pyColl[0].Length - 1); if (!listPinyin.Contains(strPinyin)) sb.Append(strPinyin); listPinyin.Add(strPinyin); } return sb.ToString(); } public static bool IsValidChar(char ch) { return ChineseChar.IsValidChar(ch); } /// <summary> /// 是否为有效的中文字 /// </summary> /// <param name="chn"></param> /// <returns></returns> public static bool IsValidChinese(string chn) { if (chn == null) return false; foreach (char c in chn) { if (!IsValidChar(c)) return false; } return true; } #endregion /// <summary> /// 生成助记码 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public static string CreateNnemonicCode(string strName) { string strPin = strName; if (string.IsNullOrEmpty(strName) == false) { string text = strName; char[] c = text.ToCharArray(); string strSum = ""; for (int i = 0; i < c.Length; i++) { //是否是汉字 if (c[i] >= 0x4e00 && c[i] <= 0x9fbb) { string strTemp = ""; strTemp = c[i].ToString(); strTemp = MyTool.PinYinConver.ConvertToFirstPinYin(strTemp); strSum += strTemp.ToUpper(); } else if (Char.IsUpper(c[i]) || Char.IsLower(c[i])) { //如果是大小写字母 strSum += c[i].ToString().ToUpper(); } else if (!Char.IsUpper(c[i]) && !Char.IsLower(c[i]) && !((c[i] >= 0x4e00 && c[i] <= 0x9fbb))) { //其它的原样 strSum += c[i].ToString(); } } strPin = strSum; //strPin = strSum.ToUpper(); } else { strPin = ""; } return strPin; } /// <summary> /// 生成拼音编码 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public static string CreateSpellCode(string strName) { string strPin = strName; if (string.IsNullOrEmpty(strName) == false) { string text = strName; char[] c = text.ToCharArray(); string strSum = ""; for (int i = 0; i < c.Length; i++) { //是否是汉字 if (c[i] >= 0x4e00 && c[i] <= 0x9fbb) { string strTemp = ""; strTemp = c[i].ToString(); strTemp = MyTool.PinYinConver.ConvertToPinYin(strTemp); strSum += strTemp.ToLower(); } else if (Char.IsUpper(c[i]) || Char.IsLower(c[i])) { //如果是大小写字母 strSum += c[i].ToString().ToLower(); } else if (!Char.IsUpper(c[i]) && !Char.IsLower(c[i]) && !((c[i] >= 0x4e00 && c[i] <= 0x9fbb))) { //其它的原样 strSum += c[i].ToString(); } } strPin = strSum; //strPin = strSum.ToLower(); } else { strPin = ""; } return strPin; } } }
调用时示例:
注:调用时最好写在KeyUp事件中
private void txtMaterialName_KeyUp(object sender, KeyEventArgs e)
{
//传入待处理的字符串或文本框内容,生成助记码
this.txtNnemonicCode.Text = MyTool.PinYinConver.CreateNnemonicCode(this.txtMaterialName.Text);
//传入待处理的字符串或文本框内容,生成拼音编码
this.txtSpellCode.Text = MyTool.PinYinConver.CreateSpellCode(this.txtMaterialName.Text);
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。