[C#] 计算汉字字符串拼音首字母 →→→→→进入此内容的聊天室

来自 , 2019-05-15, 写在 C#, 查看 118 次.
URL http://www.code666.cn/view/49228483
  1. /// <summary>
  2. /// 返回汉字字符串拼音首字母
  3. /// </summary>
  4. /// <param name="strText">汉字字符串</param>
  5.  
  6. /// <returns>返回由strText中字符及汉字部分的拼音首字母构成的字符串</returns>
  7. static public 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. /// <summary>
  18. /// 返回一个汉字的拼音首字母
  19. /// </summary>
  20. /// <param name="cnChar"></param>
  21.  
  22. /// <returns></returns>
  23. static string getSpell(string cnChar)
  24. {
  25.     byte[] arrCN = Encoding.Default.GetBytes(cnChar);
  26.     if (arrCN.Length > 1)
  27.     {
  28.         int area = (short)arrCN[0];
  29.         int pos = (short)arrCN[1];
  30.         int code = (area << 8) + pos;
  31.         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 };
  32.         for (int i = 0; i < 26; i++)
  33.         {
  34.             int max = 55290;
  35.             if (i != 25) max = areacode[i + 1];
  36.             if (areacode[i] <= code && code < max)
  37.             {
  38.                 return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
  39.             }
  40.         }
  41.         return "*";
  42.     }
  43.     else return cnChar;
  44. }
  45. //csharp/1119

回复 "计算汉字字符串拼音首字母"

这儿你可以回复上面这条便签

captcha