公用方法:得到汉字的首字母(大写)

本文介绍了一种通过C#实现的方法,用于提取中文字符串中每个汉字对应的拼音首字母,并将其转换为大写英文字符。这种方法可以应用于各种场景,如中文姓名的拼音缩写生成等。文中提供了一个具体的示例,展示了如何将混合中文与非中文字符的字符串转换为首字母缩写。

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

#region ### 得到首字母  方案1
        public static string GetChineseFirstChar(string chineseStr)
        {
            StringBuilder sb = new StringBuilder();
            int length = chineseStr.Length;
            for (int i = 0; i < length; i++)
            {
                char chineseChar = chineseStr[i];
                sb.Append(GetPYChar(chineseChar));
            }
            return sb.ToString();
        }

        private static string GetPYChar(char c)
        {
            //int ascCode = Microsoft.VisualBasic.Strings.Asc(c);

            //int temp = 65536 + ascCode;
            int temp = 65536 + GetAsciiCode(c);
            //int temp = 65536 + Convert.ToInt16(c);
            if (temp >= 45217 && temp <= 45252)
            {
                return "A";
            }
            else if (temp >= 45253 && temp <= 45760)
            {
                return "B";
            }
            else if (temp >= 45761 && temp <= 46317)
            {
                return "C";
            }
            else if (temp >= 46318 && temp <= 46825)
            {
                return "D";
            }
            else if (temp >= 46826 && temp <= 47009)
            {
                return "E";
            }
            else if (temp >= 47010 && temp <= 47296)
            {
                return "F";
            }
            else if (temp >= 47297 && temp <= 47613)
            {
                return "G";
            }
            else if (temp >= 47614 && temp <= 48118)
            {
                return "H";
            }
            else if (temp >= 48119 && temp <= 49061)
            {
                return "J";
            }
            else if (temp >= 49062 && temp <= 49323)
            {
                return "K";
            }
            else if (temp >= 49324 && temp <= 49895)
            {
                return "L";
            }
            else if (temp >= 49896 && temp <= 50370)
            {
                return "M";
            }
            else if (temp >= 50371 && temp <= 50613)
            {
                return "N";
            }
            else if (temp >= 50614 && temp <= 50621)
            {
                return "O";
            }
            else if (temp >= 50622 && temp <= 50905)
            {
                return "P";
            }
            else if (temp >= 50906 && temp <= 51386)
            {
                return "Q";
            }
            else if (temp >= 51387 && temp <= 51445)
            {
                return "R";
            }
            else if (temp >= 51446 && temp <= 52217)
            {
                return "S";
            }
            else if (temp >= 52218 && temp <= 52697)
            {
                return "T";
            }
            else if (temp >= 52698 && temp <= 52979)
            {
                return "W";
            }
            else if (temp >= 52980 && temp <= 53688)
            {
                return "X";
            }
            else if (temp >= 53689 && temp <= 54480)
            {
                return "Y";
            }
            else if (temp >= 54481 && temp <= 62289)
            {
                return "Z";
            }
            else
            {
                return c.ToString();
            }
        }
        /// <summary>
        /// 得到字符的ascii码
        /// </summary>
        private static int GetAsciiCode(char chr)
        {
            Encoding ecode = Encoding.GetEncoding("gb18030");
            byte[] codebytes = ecode.GetBytes(chr.ToString());
            if (IsTwoByteChar(chr))
            {
                // 双字节码为高位乘256,再加低位
                // 该为无符号码,再减65536
                return (int)codebytes[0] * 256 + (int)codebytes[1] - 65536;
            }
            else
            {
                return (int)codebytes[0];
            }
        }

        /// <summary>
        /// 是否为双字节字符。
        /// </summary>
        private static bool IsTwoByteChar(char chr)
        {
            string str = chr.ToString();
            // 使用中文支持编码
            Encoding ecode = Encoding.GetEncoding("gb18030");
            if (ecode.GetByteCount(str) == 2)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

调用方法:

            string ss = Common.GetChineseFirstChar("我a*%爱你中国");       //Wa*%ANZG

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值