ChineseWord.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System.Text;
  2. namespace Core.Mes.Client.Comm.Tool
  3. {
  4. public class ChineseWord
  5. {
  6. //获取汉字拼音的第一个字母
  7. public static string GetChineseSpell(string strText)
  8. {
  9. int len = strText.Length;
  10. string myStr = "";
  11. for (int i = 0; i < len; i++)
  12. {
  13. myStr += getSpell(strText.Substring(i, 1));
  14. }
  15. return myStr;
  16. }
  17. public static string[] GetChineseSpell(string[] strText)
  18. {
  19. int len = strText.Length;
  20. string[] myStr = null;
  21. for (int i = 0; i < len; i++)
  22. {
  23. myStr[i] = getSpell(strText[i]);
  24. }
  25. return myStr;
  26. }
  27. public static string getSpell(string cnChar)
  28. {
  29. byte[] arrCN = Encoding.Default.GetBytes(cnChar);
  30. if (arrCN.Length > 1)
  31. {
  32. int area = (short)arrCN[0];
  33. int pos = (short)arrCN[1];
  34. int code = (area << 8) + pos;
  35. 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 };
  36. for (int i = 0; i < 26; i++)
  37. {
  38. int max = 55290;
  39. if (i != 25) max = areacode[i + 1];
  40. if (areacode[i] <= code && code < max)
  41. {
  42. return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
  43. }
  44. }
  45. return "*";
  46. }
  47. else return cnChar;
  48. }
  49. }
  50. }