C#获取汉字首字母

		private void Button_Click(object sender, RoutedEventArgs e)
        {
            bt_test.Content = GetStringFirstChar("啊比草第饿佛给好机可里米你哦屁去肉是他我想有在");
        }


        ///<summary>
        /// 获取字符串首字母
        /// </summary>
        public static string GetStringFirstChar(string str)
        {
            string returnStr = "";
            if (string.IsNullOrEmpty(str))
            {
                return "";
            }
            byte[] array = new byte[2];
            array = Encoding.Default.GetBytes(str);
            for (int i = 0; i < array.Length; i+=2)
            {
                int j = (short)(array[i] - '\0') * 256 + ((short)(array[i + 1] - '\0'));
                if (j < 0xB0A1)
                {
                    returnStr += "";
                }
                else if (j < 0xB0C5)
                {
                    returnStr += "a";
                }
                else if (j < 0xB2C1)
                {
                    returnStr += "b";
                }
                else if (j < 0xB4EE)
                {
                    returnStr += "c";
                }
                else if (j < 0xB6EA)
                {
                    returnStr += "d";
                }
                else if (j < 0xB7A2)
                {
                    returnStr += "e";
                }
                else if (j < 0xB8C1)
                {
                    returnStr += "f";
                }
                else if (j < 0xB9FE)
                {
                    returnStr += "g";
                }
                else if (j < 0xBBF7)
                {
                    returnStr += "h";
                }
                else if (j < 0xBFA6)
                {
                    returnStr += "j";
                }
                else if (j < 0xC0AC)
                {
                    returnStr += "k";
                }
                else if (j < 0xC2E8)
                {
                    returnStr += "l";
                }
                else if (j < 0xC4C3)
                {
                    returnStr += "m";
                }
                else if (j < 0xC5B6)
                {
                    returnStr += "n";
                }
                else if (j < 0xC5BE)
                {
                    returnStr += "o";
                }
                else if (j < 0xC6DA)
                {
                    returnStr += "p";
                }
                else if (j < 0xC8BB)
                {
                    returnStr += "q";
                }
                else if (j < 0xC8F6)
                {
                    returnStr += "r";
                }
                else if (j < 0xCBFA)
                {
                    returnStr += "s";
                }
                else if (j < 0xCDDA)
                {
                    returnStr += "t";
                }
                else if (j < 0xCEF4)
                {
                    returnStr += "w";
                }
                else if (j < 0xD1B9)
                {
                    returnStr += "x";
                }
                else if (j < 0xD4D1)
                {
                    returnStr += "y";
                }
                else if (j < 0xD7FA)
                {
                    returnStr += "z";
                }
                else
                {
                    returnStr += "";
                }
            }
            return returnStr;
            
        }

在这里插入图片描述
点击按钮,效果实际如图,汉字首字母都获取到了

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值