C#如何获取文字的首字母
来源:本站原创
作者:管理员
添加时间:2011-06-09
点击数:2240
1、相关介绍:
在用C#编程时,常常会用到根据汉字来自动识别字母,下面这段代码就是介绍如何通过程序来实现首字母的功能:
以下是引用片段: public string GetPYString(string str) { string tempStr = ""; foreach (char c in str) { if ((int)c >= 33 && (int)c <= 126 || (int)c == 32 || (int)c == 65289 || (int)c == 65288) //(int) c == 65289 || (int) c == 65288 当输入全角括号时 {//字母和符号原样保留 if ((int)c == 65289 || (int)c == 65288) { if ((int)c == 65288) tempStr += "(";//输入全角括号时变为半角 if ((int)c == 65289) tempStr += ")"; } else tempStr += c.ToString(); } else {//累加拼音声母 tempStr += GetPYChar(c.ToString()); } } return tempStr; } /// /// 取单个字符的拼音声母 /// 要转换的单个汉字 /// 拼音声母 public string GetPYChar(string c) { byte[] array = new byte[2]; array = System.Text.Encoding.Default.GetBytes(c); int i = (short)(array[0] - '\0') * 256 + ((short)(array[1] - '\0')); if (i == 58557) return "H";//浣字的首字母 |