您的位置:首页 >> 编程开发 >> .NET >> C# >> 正文
RSS
 

获得汉字字符串拼音首字母C#

http://www.rdxx.com 02年09月17日 11:36 互联网 我要投稿

关键词: 字符串 , 汉字 , 拼音 , C# , 字符

获取字符串中的汉字拼音首字母(根据http://blog.csdn.net/iwebsms/archive/2004/09/28/119851.aspx修改)


可能代码比较臃肿,希望有朋友再帮忙改正


  //返回给定字符串的首字母
  private String IndexCode(String IndexTxt)
  {
   String _Temp=null;
   for(int i=0;i<IndexTxt.Length;i++)
    _Temp = _Temp + GetOneIndex( IndexTxt.Substring(i,1) );
   return _Temp;
  }


  //得到单个字符的首字母
  private String GetOneIndex(String OneIndexTxt)
  {
   if(Convert.ToChar(OneIndexTxt)>=0 && Convert.ToChar(OneIndexTxt)<256)
    return OneIndexTxt;
   else
   {
    Encoding gb2312 = Encoding.GetEncoding("gb2312");
    byte[] unicodeBytes = Encoding.Unicode.GetBytes(OneIndexTxt);
    byte[] gb2312Bytes = Encoding.Convert(Encoding.Unicode, gb2312, unicodeBytes);
                return GetX(Convert.ToInt32(
     String.Format("{0:D2}",Convert.ToInt16(gb2312Bytes[0])-160)
     + String.Format("{0:D2}",Convert.ToInt16(gb2312Bytes[1])-160)
     ));
   }
 
  }
  

  //根据区位得到首字母
  private String GetX(int GBCode)
  {   
   if(GBCode >= 1601 && GBCode < 1637)return "A";
   if(GBCode >= 1637 && GBCode < 1833)return "B";
   if(GBCode >= 1833 && GBCode < 2078)return "C";
   if(GBCode >= 2078 && GBCode < 2274)return "D";
   if(GBCode >= 2274 && GBCode < 2302)return "E";
   if(GBCode >= 2302 && GBCode < 2433)return "F";
   if(GBCode >= 2433 && GBCode < 2594)return "G";
   if(GBCode >= 2594 && GBCode < 2787)return "H";
   if(GBCode >= 2787 && GBCode < 3106)return "J";
   if(GBCode >= 3106 && GBCode < 3212)return "K";
   if(GBCode >= 3212 && GBCode < 3472)return "L";
   if(GBCode >= 3472 && GBCode < 3635)return "M";
   if(GBCode >= 3635 && GBCode < 3722)return "N";
   if(GBCode >= 3722 && GBCode < 3730)return "O";
   if(GBCode >= 3730 && GBCode < 3858)return "P";
   if(GBCode >= 3858 && GBCode < 4027)return "Q";
   if(GBCode >= 4027 && GBCode < 4086)return "R";
   if(GBCode >= 4086 && GBCode < 4390)return "S";
   if(GBCode >= 4390 && GBCode < 4558)return "T";
   if(GBCode >= 4558 && GBCode < 4684)return "W";

9 7 3 1 2 3 4 8 :

 
 
标签: 字符串 , 汉字 , 拼音 , C# , 字符 打印本文
 
 
  热点搜索
 
 
 



Valid XHTML 1.0 Transitional
Copyright ©2005 - 2008 Rdxx.Com,All Rights Reserved
收藏本页
收藏本站