日期:2014-05-17  浏览次数:20470 次

求助ASP.NET怎么实现同音字查询
如题比如 我输入 “里斯” 也能查询到“李四”?


------解决方案--------------------
给楼主一个方法(网上找的),用来生成拼音
C# code

    /// <summary>   
            /// 生成拼音简码   
            /// </summary>   
            /// <param name="unicodeString">Unicode编码字符串</param>   
            /// <returns>拼音简码:string</returns>   
            public   static   string  GetPinyinCode( string  unicodeString)  
            {  
                int  i = 0;  
                ushort  key = 0;  
                string  strResult =  string .Empty;             //创建两个不同的encoding对象   
                Encoding unicode = Encoding.Unicode;  
                //创建GBK码对象   
                Encoding gbk = Encoding.GetEncoding(936);  
                //将unicode字符串转换为字节   
                byte [] unicodeBytes = unicode.GetBytes(unicodeString);  
                //再转化为GBK码   
                byte [] gbkBytes = Encoding.Convert(unicode, gbk, unicodeBytes);  
                while  (i < gbkBytes.Length)  
                {  
                    //如果为数字/字母/其他ASCII符号   
                    if  (gbkBytes <= 127)  
                    {  
                        strResult = strResult + (char )gbkBytes;  
                        i++;  
                    }  
                    #region 否则生成汉字拼音简码,取拼音首字母   
                    else   
                    {  
                        key = (ushort )(gbkBytes * 256 + gbkBytes[i + 1]);  
                        if  (key >=  '/uB0A1'  && key <=  '/uB0C4' )  
                        {  
                            strResult = strResult + "A" ;  
                        }  
                        else   if  (key >=  '/uB0C5'  && key <=  '/uB2C0' )  
                        {  
                            strResult = strResult + "B" ;  
                        }  
                        else   if  (key >=  '/uB2C1'  && key <=  '/uB4ED' )  
                        {  
                            strResult = strResult + "C" ;  
                        }  
                        else   if  (key >=  '/uB4EE'  && key <=  '/uB6E9' )  
                        {  
                            strResult = strResult + "D" ;  
                        }  
                        else   if  (key >=  '/uB6EA'  && key <=  '/uB7A1' )  
                        {  
                            strResult = strResult + "E" ;  
                        }  
                        else   if  (key >=  '/uB7A2'  && key <=  '/uB8C0' )  
                        {  
                            strResult = strResult + "F" ;  
                        }  
                        else   if  (key >=  '/uB8C1'  && key <=  '/uB9FD' )  
                        {  
                            strResult = strResult + "G" ;  
                        }  
                        else   if  (key >=  '/uB9FE'  && key <=  '/uBBF6' )  
                        {  
                            strResult = strResult + "H" ;  
                        }  
                        else   if  (key >=  '/uBBF7'  && key <=  '/uBFA5' )  
                        {  
                            strResult = strResult + "J" ;  
                        }  
                        else   if  (key >=  '/uBFA6'  && key <=  '/uC0AB' )  
                        {  
                            strResult = strResult + "K" ;  
                        }  
                        else   if  (key >=  '/uC0AC'  && key <=  '/uC2E7' )  
                        {  
                            strResult = strResult + "L" ;  
                        }  
                        else   if  (key >=  '/uC2E8'  && key <=  '/uC4C