将汉字转换为拼音缩写的代码(C#) (转自:torome)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. namespace getPageValue
  8. {
  9.     public partial class WebForm1 : System.Web.UI.Page
  10.     {
  11.         protected void Page_Load(object sender, EventArgs e)
  12.         {
  13.             Response.Write(GetPYString("中华人民共和国"));
  14.         }
  15.         public string GetPYString(string str)
  16.         {
  17.             string tempStr = "";
  18.             foreach (char c in str)
  19.             {
  20.                 if ((int)c >= 33 && (int)c <= 126)
  21.                 {//字母和符号原样保留
  22.                     tempStr += c.ToString();
  23.                 }
  24.                 else
  25.                 {//累加拼音声母
  26.                     tempStr += GetPYChar(c.ToString());
  27.                 }
  28.             }
  29.             return tempStr;
  30.         }
  31.         /// <summary>
  32.         /// 取单个字符的拼音声母
  33.         /// Code By MuseStudio@hotmail.com
  34.         /// 2004-11-30
  35.         /// </summary>
  36.         /// <param name="c">要转换的单个汉字</param>
  37.         /// <returns>拼音声母</returns>
  38.         public string GetPYChar(string c)
  39.         {
  40.             byte[] array = new byte[2];
  41.             array = System.Text.Encoding.Default.GetBytes(c);
  42.             int i = (short)(array[0] - '/0') * 256 + ((short)(array[1] - '/0'));
  43.             if (i < 0xB0A1) return "*";
  44.             if (i < 0xB0C5) return "a";
  45.             if (i < 0xB2C1) return "b";
  46.             if (i < 0xB4EE) return "c";
  47.             if (i < 0xB6EA) return "d";
  48.             if (i < 0xB7A2) return "e";
  49.             if (i < 0xB8C1) return "f";
  50.             if (i < 0xB9FE) return "g";
  51.             if (i < 0xBBF7) return "h";
  52.             if (i < 0xBFA6) return "g";
  53.             if (i < 0xC0AC) return "k";
  54.             if (i < 0xC2E8) return "l";
  55.             if (i < 0xC4C3) return "m";
  56.             if (i < 0xC5B6) return "n";
  57.             if (i < 0xC5BE) return "o";
  58.             if (i < 0xC6DA) return "p";
  59.             if (i < 0xC8BB) return "q";
  60.             if (i < 0xC8F6) return "r";
  61.             if (i < 0xCBFA) return "s";
  62.             if (i < 0xCDDA) return "t";
  63.             if (i < 0xCEF4) return "w";
  64.             if (i < 0xD1B9) return "x";
  65.             if (i < 0xD4D1) return "y";
  66.             if (i < 0xD7FA) return "z";
  67.             return "*";
  68.         }
  69.     }
  70. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值