【语言-c#】字符串转拼音码

 直接上代码

        /// <summary>
        /// 1、先获取默认 Unicode 的编码
        /// 2、将 Unicode 编码转换成 GBK 编码
        /// 3、遍历GBK编码,根据 GBK 的编码特性获取数字、字母、各区间的拼音首字母。
        /// </summary>
        /// <param name="unicodeString"></param>
        /// <returns></returns>
        public static string ToPinYin(string unicodeString)
        {
            string strResult = string.Empty;
            try
            {
                int i = 0;
                int key = 0;

                Encoding unicode = Encoding.Unicode;
                Encoding gbk = Encoding.GetEncoding("GBK");
                byte[] unicodeBytes = unicode.GetBytes(unicodeString);
                byte[] gbkBytes = Encoding.Convert(unicode, gbk, unicodeBytes);
                while (i < gbkBytes.Length)
                {
                    if (gbkBytes[i] <= 127)
                    {
                        if('0'<= gbkBytes[i] && gbkBytes[i] <= '9')
                        {
                            strResult = strResult + (char)gbkBytes[i];
                        }
                        else if ('a'<= gbkBytes[i] && gbkBytes[i] <= 'z')
                        {
                            strResult = strResult + (char)gbkBytes[i];
                        }
                        if ('A'<= gbkBytes[i] && gbkBytes[i] <= 'Z')
                        {
                            strResult = strResult + (char)gbkBytes[i];
                        }
                        i++;
                    }
                    else
                    {

                        key = ((ushort)gbkBytes[i] )<< 8 | gbkBytes[i + 1];
                        if (key >= 0xB0A1 && key <= 0xB0C4)
                        {
                            strResult = strResult + "A";
                        }
                        else if (key >= 0xB0C5 && key <= 0xB2C0)
                        {
                            strResult = strResult + "B";
                        }
                        else if (key >= 0xB2C1 && key <= 0xB4ED)
                        {
                            strResult = strResult + "C";
                        }
                        else if (key >= 0xB4EE && key <= 0xB6E9)
                        {
                            strResult = strResult + "D";
                        }
                        else if (key >= 0xB6EA && key <= 0xB7A1)
                        {
                            strResult = strResult + "E";
                        }
                        else if (key >= 0xB7A2 && key <= 0xB8C0)
                        {
                            strResult = strResult + "F";
                        }
                        else if (key >= 0xB8C1 && key <= 0xB9FD)
                        {
                            strResult = strResult + "G";
                        }
                        else if (key >= 0xB9FE && key <= 0xBBF6)
                        {
                            strResult = strResult + "H";
                        }
                        else if (key >= 0xBBF7 && key <= 0xBFA5)
                        {
                            strResult = strResult + "J";
                        }
                        else if (key >= 0xBFA6 && key <= 0xC0AB)
                        {
                            strResult = strResult + "K";
                        }
                        else if (key >= 0xC0AC && key <= 0xC2E7)
                        {
                            strResult = strResult + "L";
                        }
                        else if (key >= 0xC2E8 && key <= 0xC4C2)
                        {
                            strResult = strResult + "M";
                        }
                        else if (key >= 0xC4C3 && key <= 0xC5B5)
                        {
                            strResult = strResult + "N";
                        }
                        else if (key >= 0xC5B6 && key <= 0xC5BD)
                        {
                            strResult = strResult + "O";
                        }
                        else if (key >= 0xC5BE && key <= 0xC6D9)
                        {
                            strResult = strResult + "P";
                        }
                        else if (key >= 0xC6DA && key <= 0xC8BA)
                        {
                            strResult = strResult + "Q";
                        }
                        else if (key >= 0xC8BB && key <= 0xC8F5)
                        {
                            strResult = strResult + "R";
                        }
                        else if (key >= 0xC8F6 && key <= 0xCBF9)
                        {
                            strResult = strResult + "S";
                        }
                        else if (key >= 0xCBFA && key <= 0xCDD9)
                        {
                            strResult = strResult + "T";
                        }
                        else if (key >= 0xCDDA && key <= 0xCEF3)
                        {
                            strResult = strResult + "W";
                        }
                        else if (key >= 0xCEF4 && key <= 0xD188)
                        {
                            strResult = strResult + "X";
                        }
                        else if (key >= 0xD1B9 && key <= 0xD4D0)
                        {
                            strResult = strResult + "Y";
                        }
                        else if (key >= 0xD4D1 && key <= 0xD7F9)
                        {
                            strResult = strResult + "Z";
                        }
                        else
                        {
                           // strResult = strResult + "?";
                        }
                        i = i + 2;
                    }
                }
                return strResult;
            }
            catch (System.OverflowException)
            {
                return strResult;
            }
            catch (Exception)
            {
                return strResult;
            }
        }

 


版权声明:本文为chenlu5201314原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。