logo 罗植峰个人博客

再过好多年,在白云悬碧空的秋日,在夕阳恋红叶的午后,当我重新翻开你的那一页时,依然会感受到潮湿的印痕。

技术

c#获取汉字拼音

        评论:   

            
    public class GetStringSpell
    {
       /// <summary>
       /// 返回字母
       /// </summary>
       /// <param name="str"></param>
       /// <returns></returns>
      public static string GetChineseSpell(string str)
      {
          Match m = Regex.Match(str, "[\u4e00-\u9fa5]");//验证是否是汉字

          if (m.Success)
          {
              int len = str.Length;
              string mystr = "";
              for (int i = 0; i < len; i  )
              {
                  mystr  = getSpell(str.Substring(i, 1));
              }
              return mystr;
          }
          else {
              return str;
          }
      }
        /// <summary>
        /// 小写转换
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
      public static string GetULowerChineseSpell(string str)
      {
          return GetChineseSpell(str).ToLower();
      }
       /// <summary>
       /// 大写转换
       /// </summary>
       /// <param name="str"></param>
       /// <returns></returns>
      public static string GetUpperChineseSpell(string str)
      {
          return GetChineseSpell(str).ToUpper();
      }
        /// <summary>
        /// 获取汉字首字母
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
      public static string getSpell(string str)
      {
          byte[] arrCN = System.Text.Encoding.Default.GetBytes(str);
          if (arrCN.Length > 0)
          {
              int area = (short)arrCN[0];
              int pos = (short)arrCN[1];
              int code = (area << 8)   pos;
              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 System.Text.Encoding.Default.GetString(new byte[] { (byte)(65   i)});
                  }
              }
              return "*";
          }
          else
          {
              return str;
          }
      }


    }


转载及版权说明:本站文章由罗植峰个人博客所有,转载请注明出处。


您的评价是对我最大的支持

选择头像

评论

条评论