指定汉字字符串中检索符合拼音索引字符串

        /// <summary>
        /// 指定汉字字符串中检索符合拼音索引字符串
        /// </summary>
        /// <param name="chineseCharacters">汉字字符串</param>
        /// <param name="isControlCharacter">是否返回控制字符</param>
        /// <returns>汉语拼音相对应首字母集合</returns>
        public static List<(string, string)> GetChineseSpellCode(List<string> chineseCharacters, bool isControlCharacter = false)
        {
            var result = new List<(string, string)>();
            try
            {
                foreach (var tempString in chineseCharacters)
                {
                    var tempItem2 = string.Empty;
                    foreach (var tempItem1 in tempString)
                    {
                        tempItem2 += GetChineseLetter(tempItem1.ToString(), isControlCharacter);
                    }
                    result.Add((tempString, tempItem2));
                }
            }
            catch { }
            return result;
        }

        /// <summary>
        /// 得到一个汉字的拼音第一个字母,如果是一个英文字母则直接返回大写字母
        /// </summary>
        /// <param name="chineseCharacters">单个汉字</param>
        /// <param name="isControlCharacter">是否返回控制字符</param>
        /// <returns>单个大写字母</returns>
        private static string GetChineseLetter(string chineseCharacters, bool isControlCharacter = false)
        {
            var result = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(chineseCharacters))
                {
                    return result;
                }
                else if (chineseCharacters.Length > 1)
                {
                    chineseCharacters = chineseCharacters.Substring(0, 1);
                }
                var tempArray = System.Text.Encoding.Default.GetBytes(chineseCharacters);
                if (tempArray.Length == 1)
                {
                    var tempChar = (int)chineseCharacters[0];
                    if (isControlCharacter)
                    {
                        /// 0~127[33个控制字符,95个可显示字符]
                        if (tempChar < 128)
                        {
                            result = chineseCharacters.ToUpper();
                        }
                    }
                    else
                    {
                        /// 数字/大写字母/小写字母
                        if ((tempChar >= 48 && tempChar < 57) || (tempChar >= 65 && tempChar <= 90) || (tempChar >= 97 && tempChar <= 122))
                        {
                            result = chineseCharacters.ToUpper();
                        }
                    }
                }
                else
                {
                    int value1 = (short)(tempArray[0]);
                    int value2 = (short)(tempArray[1]);
                    long charValue = value1 * 256 + value2;
                    if ((charValue >= 45217) && (charValue <= 45252))
                    {
                        result = "A";
                    }
                    else if ((charValue >= 45253) && (charValue <= 45760))
                    {
                        result = "B";
                    }
                    else if ((charValue >= 45761) && (charValue <= 46317))
                    {
                        result = "C";
                    }
                    else if ((charValue >= 46318) && (charValue <= 46825))
                    {
                        result = "D";
                    }
                    else if ((charValue >= 46826) && (charValue <= 47009))
                    {
                        result = "E";
                    }
                    else if ((charValue >= 47010) && (charValue <= 47296))
                    {
                        result = "F";
                    }
                    else if ((charValue >= 47297) && (charValue <= 47613))
                    {
                        result = "G";
                    }
                    else if ((charValue >= 47614) && (charValue <= 48118))
                    {
                        result = "H";
                    }
                    else if ((charValue >= 48119) && (charValue <= 49061))
                    {
                        result = "J";
                    }
                    else if ((charValue >= 49062) && (charValue <= 49323))
                    {
                        result = "K";
                    }
                    else if ((charValue >= 49324) && (charValue <= 49895))
                    {
                        result = "L";
                    }
                    else if ((charValue >= 49896) && (charValue <= 50370))
                    {
                        result = "M";
                    }
                    else if ((charValue >= 50371) && (charValue <= 50613))
                    {
                        result = "N";
                    }
                    else if ((charValue >= 50614) && (charValue <= 50621))
                    {
                        result = "O";
                    }
                    else if ((charValue >= 50622) && (charValue <= 50905))
                    {
                        result = "P";
                    }
                    else if ((charValue >= 50906) && (charValue <= 51386))
                    {
                        result = "Q";
                    }
                    else if ((charValue >= 51387) && (charValue <= 51445))
                    {
                        result = "R";
                    }
                    else if ((charValue >= 51446) && (charValue <= 52217))
                    {
                        result = "S";
                    }
                    else if ((charValue >= 52218) && (charValue <= 52697))
                    {
                        result = "T";
                    }
                    else if ((charValue >= 52698) && (charValue <= 52979))
                    {
                        result = "W";
                    }
                    else if ((charValue >= 52980) && (charValue <= 53640))
                    {
                        result = "X";
                    }
                    else if ((charValue >= 53689) && (charValue <= 54480))
                    {
                        result = "Y";
                    }
                    else if ((charValue >= 54481) && (charValue <= 55289))
                    {
                        result = "Z";
                    }
                }
            }
            catch { }
            return result;
        }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值