C# 获取汉字的拼音首字母

一种是把所有中文字符集合起来组成一个对照表;另一种是依照汉字在Unicode编码表中的排序来确定拼音的首字母。碰到多音字时就以常用的为准(第一种方法中可以自行更改,方法为手动把该汉字移动到对应的拼音首字母队列,我们这里介绍第二种。

获取汉字拼音的首字母是一个在做项目的过程中经常需要用到的功能,今天我们主要来探讨下C# 获取汉字的拼音首字母

    static void Main(string[] args)
{ Console.WriteLine(GetSpellCode("asdf牛逼你水电费")) ;
Console.ReadKey();
} /// <summary> /// 在指定的字符串列表CnStr中检索符合拼音索引字符串 /// </summary> /// <param name="CnStr">汉字字符串</param> /// <returns>相对应的汉语拼音首字母串</returns> public static string GetSpellCode(string CnStr)
{ string strTemp = ""; int iLen = CnStr.Length; int i = 0; for (i = 0; i <= iLen - 1; i++)
{ strTemp += GetCharSpellCode(CnStr.Substring(i, 1)); } return strTemp; } /// <summary>
/// 得到一个汉字的拼音第一个字母,如果是一个英文字母则直接返回大写字母
/// </summary>
/// <param name="CnChar">单个汉字</param>
/// <returns>单个大写字母</returns> private static string GetCharSpellCode(string CnChar)
{ long iCnChar; byte[] ZW = System.Text.Encoding.Default.GetBytes(CnChar); //如果是字母,则直接返回 if (ZW.Length == 1)
{ return CnChar.ToUpper(); } else
{ // get the array of byte from the single char int i1 = (short)(ZW[0]); int i2 = (short)(ZW[1]); iCnChar = i1 * 256 + i2; } // iCnChar match the constant if ((iCnChar >= 45217) && (iCnChar <= 45252))
{ return "A"; } else if ((iCnChar >= 45253) && (iCnChar <= 45760))
{ return "B"; }
else if ((iCnChar >= 45761) && (iCnChar <= 46317))
{ return "C"; }
else if ((iCnChar >= 46318) && (iCnChar <= 46825))
{ return "D"; }
else if ((iCnChar >= 46826) && (iCnChar <= 47009))
{ return "E"; }
else if ((iCnChar >= 47010) && (iCnChar <= 47296))
{ return "F"; }
else if ((iCnChar >= 47297) && (iCnChar <= 47613))
{ return "G"; }
else if ((iCnChar >= 47614) && (iCnChar <= 48118))
{ return "H"; }
else if ((iCnChar >= 48119) && (iCnChar <= 49061))
{ return "J"; }
else if ((iCnChar >= 49062) && (iCnChar <= 49323))
{ return "K"; }
else if ((iCnChar >= 49324) && (iCnChar <= 49895))
{ return "L"; }
else if ((iCnChar >= 49896) && (iCnChar <= 50370))
{ return "M"; }
else if ((iCnChar >= 50371) && (iCnChar <= 50613))
{ return "N"; }
else if ((iCnChar >= 50614) && (iCnChar <= 50621))
{ return "O"; }
else if ((iCnChar >= 50622) && (iCnChar <= 50905))
{ return "P"; }
else if ((iCnChar >= 50906) && (iCnChar <= 51386))
{ return "Q"; }
else if ((iCnChar >= 51387) && (iCnChar <= 51445))
{ return "R"; }
else if ((iCnChar >= 51446) && (iCnChar <= 52217))
{ return "S"; }
else if ((iCnChar >= 52218) && (iCnChar <= 52697))
{ return "T"; }
else if ((iCnChar >= 52698) && (iCnChar <= 52979))
{ return "W"; }
else if ((iCnChar >= 52980) && (iCnChar <= 53640))
{ return "X"; }
else if ((iCnChar >= 53689) && (iCnChar <= 54480))
{ return "Y"; }
else if ((iCnChar >= 54481) && (iCnChar <= 55289))
{ return "Z"; }
else return ("?"); }

最新文章

  1. C#中常用的系统内置委托
  2. Socket用法详解
  3. RHEL7管道与重定向
  4. mapreduce job提交流程源码级分析(三)
  5. zoj 1450 Minimal Circle 最小覆盖圆
  6. 用css3实现鼠标移进去当前亮其他变灰
  7. 黑马程序员_JavaIO流(二)
  8. 权威验证:MSDN会明确告诉你下载的光盘镜像是否正宗微软原版
  9. hdu 5066 Harry And Physical Teacher(Bestcoder Round #14)
  10. 矩阵的f范数及其求偏导法则
  11. 阿里云Ubuntu下安装、配置权限和导入本地mongodb
  12. python正常时间和unix时间戳时间的相互转换源码
  13. Install Air Conditioning HDU - 4756(最小生成树+树形dp)
  14. Spring入门详细教程(二)
  15. python_str 字符串的所有方法
  16. 函数, arguments对象, eval,静态成员和实例成员
  17. PythonStudy——字符串类型 String type
  18. nginx,gunicorn常用命令
  19. Tkernel Package NCollection哈希基础的类
  20. vim的查找与替换

热门文章

  1. 基于websocket实现的web聊天室
  2. 通过串口工具下发指令的Python脚本
  3. ffmpeg推送,EasyDarwin转发,vlc播放 实现整个RTSP直播
  4. ElasticSearch(一)什么是全文检索?
  5. Android笔记之使用LocationManager获取经纬度
  6. The Little Match Girl,摘自iOS应用Snow White and more stories
  7. Delphi类的默认区域
  8. linux环境下启动tomcat7出现时间过长(已经编译完成的项目)问题解决!
  9. 对于iPhone描述文件的签名认证
  10. java to Json or Json to JavaBean