汉字获取首字母

今天贴两段用通过c#实现汉字获取首字母的方法,希望对大家有所帮助。

方法一:

ExpandedBlockStart.gif 代码
class  ChinesSpell
{

    
private   const   int  constA  =   45217 ;
    
private   const   int  constB  =   45253 ;
    
private   const   int  constC  =   45761 ;
    
private   const   int  constD  =   46318 ;
    
private   const   int  constE  =   46826 ;
    
private   const   int  constF  =   47010 ;
    
private   const   int  constG  =   47297 ;
    
private   const   int  constH  =   47614 ;
    
private   const   int  constJ  =   48119 ;
    
private   const   int  constK  =   49062 ;
    
private   const   int  constL  =   49324 ;
    
private   const   int  constM  =   49896 ;
    
private   const   int  constN  =   50371 ;
    
private   const   int  constO  =   50614 ;
    
private   const   int  constP  =   50622 ;
    
private   const   int  constQ  =   50906 ;
    
private   const   int  constR  =   51387 ;
    
private   const   int  constS  =   51446 ;
    
private   const   int  constT  =   52218 ;
    
private   const   int  constW  =   52698 ;
    
private   const   int  constX  =   52980 ;
    
private   const   int  constY  =   53689 ;
    
private   const   int  constZ  =   54481 ;

    
public  ChinesSpell()
    { }

    
public   static   string  GetCharSpellCode( string  CnChar)
    {
        
long  iCnChar;
        
byte [] HZ  =  System.Text.Encoding.Default.GetBytes(CnChar);
        
if  (HZ.Length  ==   1 )
        {
            
return  CnChar.ToUpper();
        }
        
else
        {
            
int  a1  =  ( short )(HZ[ 0 ]);
            
int  a2  =  ( short )(HZ[ 1 ]);
            iCnChar 
=  a1  *   256   +  a2;
        }
        
return  GetChar(iCnChar);
    }

    
private   static   string  GetChar( long  iCnChar)
    {
        
if  (CompareChar(iCnChar, constA, constB))
        {
            
return   " A " ;
        }
        
if  (CompareChar(iCnChar, constB, constC))
        {
            
return   " B " ;
        }
        
if  (CompareChar(iCnChar, constC, constD))
        {
            
return   " C " ;
        }
        
if  (CompareChar(iCnChar, constD, constE))
        {
            
return   " D " ;
        }
        
if  (CompareChar(iCnChar, constE, constF))
        {
            
return   " E " ;
        }
        
if  (CompareChar(iCnChar, constF, constG))
        {
            
return   " F " ;
        }
        
if  (CompareChar(iCnChar, constG, constH))
        {
            
return   " G " ;
        }
        
if  (CompareChar(iCnChar, constH, constJ))
        {
            
return   " H " ;
        }
        
if  (CompareChar(iCnChar, constJ, constK))
        {
            
return   " J " ;
        }
        
if  (CompareChar(iCnChar, constK, constL))
        {
            
return   " K " ;
        }
        
if  (CompareChar(iCnChar, constL, constM))
        {
            
return   " L " ;
        }
        
if  (CompareChar(iCnChar, constM, constN))
        {
            
return   " M " ;
        }
        
if  (CompareChar(iCnChar, constN, constO))
        {
            
return   " N " ;
        }
        
if  (CompareChar(iCnChar, constO, constP))
        {
            
return   " O " ;
        }
        
if  (CompareChar(iCnChar, constP, constQ))
        {
            
return   " P " ;
        }
        
if  (CompareChar(iCnChar, constQ, constR))
        {
            
return   " Q " ;
        }
        
if  (CompareChar(iCnChar, constR, constS))
        {
            
return   " R " ;
        }
        
if  (CompareChar(iCnChar, constS, constT))
        {
            
return   " S " ;
        }
        
if  (CompareChar(iCnChar, constT, constW))
        {
            
return   " T " ;
        }
        
if  (CompareChar(iCnChar, constW, constX))
        {
            
return   " W " ;
        }
        
if  (CompareChar(iCnChar, constX, constY))
        {
            
return   " X " ;
        }
        
if  (CompareChar(iCnChar, constY, constZ))
        {
            
return   " Y " ;
        }
        
if  (CompareChar(iCnChar, constZ,  55289 ))
        {
            
return   " Z " ;
        }
        
else
        {
            
return   " * " ;
        }
    }
    
private   static   bool  CompareChar( long  iCnChar,  int  S,  int  E)
    {
        
if  (iCnChar  >=  S  &&  iCnChar  <  E)
        {
            
return   true ;
        }
        
else
        {
            
return   false ;
        }

    }
}

 

方法二:

 

ExpandedBlockStart.gif 代码
class  ChinesSpell2
{
      
static   public   string  GetChineseSpell( string  strText)
         {
             
int  len  =  strText.Length;
             
string  myStr  =   "" ;
             
for  ( int  i  =   0 ; i  <  len; i ++ )
             {
                 myStr 
+=  getSpell(strText.Substring(i,  1 ));
             }
             
return  myStr;
         }

         
static   public   string [] GetChineseSpell( string [] strText)
         {
             
int  len  =  strText.Length;
             
string [] myStr  =   null ;
             
for  ( int  i  =   0 ; i  <  len; i ++ )
             {
                 myStr[i] 
=  getSpell(strText[i]);
             }
             
return  myStr;
         }

         
static   public   string  getSpell( string  cnChar)
         {
             
byte [] arrCN  = System.Text.Encoding.Default.GetBytes(cnChar);
             
if  (arrCN.Length  >   1 )
             {
                 
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  cnChar;
         }

}

 

 

转载于:https://www.cnblogs.com/valin/archive/2010/08/31/1813415.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值