下午封装了一个字符或汉字获取第一个单词的类 using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; namespace Nimeux.MyCOM ... { public class WordTransform ...{ public WordTransform() ...{ } /**//// <summary> /// 输入一个中文字,返回拼音的第一个小写字母 /// </summary> /// <param name="cn">输入的中文字</param> /// <returns></returns> static public string WordSpell(string cn) ...{ byte[] arrCN = Encoding.Default.GetBytes(cn); if (arrCN.Length > 1) ...{ int firstWord = (short)arrCN[0]; int secondWord = (short)arrCN[1]; int code = (firstWord << 8) + secondWord; int[] areacode = ...{ 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 }; for (int i = 0; i < 26; i++) ...{ int max = 55290; if (i != 25) max = areacode[i + 1]; if (areacode[i] <= code && code < max) ...{ //获取大写字母 return Encoding.Default.GetString(new byte[] ...{ (byte)(97 + i) }); } } return "*"; } else return cn; } /**//// <summary> /// 输入一组中文,输出每个中文字拼音的小写 /// </summary> /// <param name="cns">一组中文</param> /// <returns></returns> static public string WordsSpell(string cns) ...{ int num = cns.Length; string result = ""; for (int i = 0; i < num; i++) ...{ result += WordSpell(cns.Substring(i, 1)); } return result; } /**//// <summary> /// 判断输入的字符串是否是汉字 /// </summary> /// <param name="str">输入的字符串</param> /// <returns></returns> static public bool MatchCN(string str) ...{ string flag = @"[一-龥]"; Regex rg = new Regex(flag); if (rg.IsMatch(str)) ...{ return true; } else ...{ return false; } } /**//// <summary> /// 提取输入字符串的第一个字母,如果是中文则提取拼音的第一个字母 /// </summary> /// <param name="input">输入字符串</param> /// <returns></returns> static public string Transform(string input) ...{ string temp = string.Format(input); if (MatchCN(temp)) ...{ return WordSpell(temp); } else ...{ string result = ""; for (int i = 0; i < 26; i++) ...{ if (temp.Substring(0, 1) == Encoding.Default.GetString(new byte[] ...{ (byte)(65 + i) })) ...{ result = Encoding.Default.GetString(new byte[] ...{ (byte)(97 + i) }); break; } else ...{ result = temp.Substring(0, 1); } } return result; } } }}