• c# 汉字转拼音


    方法一: 
    
    /// <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 <= 'uC4C2')   
                        {   
                            strResult = strResult + "M";   
                        }   
                        else if (key >= 'uC4C3' && key <= 'uC5B5')   
                        {   
                            strResult = strResult + "N";   
                        }   
                        else if (key >= 'uC5B6' && key <= 'uC5BD')   
                        {   
                            strResult = strResult + "O";   
                        }   
                        else if (key >= 'uC5BE' && key <= 'uC6D9')   
                        {   
                            strResult = strResult + "P";   
                        }   
                        else if (key >= 'uC6DA' && key <= 'uC8BA')   
                        {   
                            strResult = strResult + "Q";   
                        }   
                        else if (key >= 'uC8BB' && key <= 'uC8F5')   
                        {   
                            strResult = strResult + "R";   
                        }   
                        else if (key >= 'uC8F6' && key <= 'uCBF9')   
                        {   
                            strResult = strResult + "S";   
                        }   
                        else if (key >= 'uCBFA' && key <= 'uCDD9')   
                        {   
                            strResult = strResult + "T";   
                        }   
                        else if (key >= 'uCDDA' && key <= 'uCEF3')   
                        {   
                            strResult = strResult + "W";   
                        }   
                        else if (key >= 'uCEF4' && key <= 'uD188')   
                        {   
                            strResult = strResult + "X";   
                        }   
                        else if (key >= 'uD1B9' && key <= 'uD4D0')   
                        {   
                            strResult = strResult + "Y";   
                        }   
                        else if (key >= 'uD4D1' && key <= 'uD7F9')   
                        {   
                            strResult = strResult + "Z";   
                        }   
                        else  
                        {   
                            strResult = strResult + "?";   
                        }   
                        i = i + 2;   
                    }  
                    #endregion   
                }//end while   
                return strResult;   
            }  
    
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    
    方法二: 
    
    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 getSpell(string cnChar)   
    {   
    byte[] arrCN = 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 Encoding.Default.GetString(new byte[]{(byte)(65+i)});   
    }   
    }   
    return "*";   
    }   
    else return cnChar;   
    }  
    
    
    
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  • 相关阅读:
    AKS (6) 新建Azure AKS Node Pool,并扩容Node磁盘容量
    Azure Platform Introduction (16) 不同订阅之间可用区的对应关系
    基于Spring Cache实现二级缓存(Caffeine+Redis)
    钉钉机器人实现异常预警通知功能
    Android GPS定位详解
    算法思想篇递归
    算法思想篇排序
    win10如何在桌面上显示或隐藏计算机、回收站图标
    如何解决“未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序”问题
    IIS/项目访问出现“HTTP 错误 401.3 Unauthorized”问题,如何解决
  • 原文地址:https://www.cnblogs.com/alaric888/p/4940109.html
Copyright © 2020-2023  润新知