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

本文介绍了一种将Unicode字符串转换为GBK编码,并从中提取数字、字母及汉字拼音首字母的方法。通过使用C#编程语言,详细展示了编码转换的过程,包括如何遍历GBK编码并根据其特性获取特定字符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 直接上代码

        /// <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;
            }
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值