zoukankan      html  css  js  c++  java
  • C#获取汉字拼写首字母

            #region 获取首字母
            /// <summary>
            /// 在指定的字符串列表CnStr中检索符合拼音索引字符串
            /// </summary>
            /// <param name="CnStr">汉字字符串</param>
            /// <returns>相对应的汉语拼音首字母串</returns>
            public static string GetFirstCode(string CnStr)
            {
                string Surname = CnStr.Substring(0, 1);
                string strTemp = GetSpellCode(Surname);
                return strTemp;
            }
    
            /// <summary>
            /// 得到一个汉字的拼音第一个字母,如果是一个英文字母则直接返回大写字母
            /// </summary>
            /// <param name="CnChar">单个汉字</param>
            /// <returns>单个大写字母</returns>
            private static string GetSpellCode(string CnChar)
            {
                long iCnChar;
                byte[] arrCN = System.Text.Encoding.Default.GetBytes(CnChar);
    
                //如果是字母,则直接返回
                if (arrCN.Length == 1)
                {
                    CnChar = CnChar.ToUpper();
                }
                else
                {
                    int area = (short)arrCN[0];
                    int pos = (short)arrCN[1];
                    iCnChar = (area << 8) + pos;
    
                    // iCnChar match the constant
                    string letter = "ABCDEFGHJKLMNOPQRSTWXYZ";
                    int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 
                        48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906,
                        51387, 51446, 52218, 52698, 52980, 53689, 54481, 55290 }; for (int i = 0; i < 23; i++) { if (areacode[i] <= iCnChar && iCnChar < areacode[i + 1]) { CnChar = letter.Substring(i, 1); break; } } } return CnChar; } #endregion
  • 相关阅读:
    第八周总结和实验六
    第七周总结与实验五
    遍历目录中的所有文件和目录,并生成全路径
    python watchdog
    Offer_answer_with_SDP_rfc3264
    [转]UML八大误解
    leetcode周赛220
    Codeforces Round #690 (Div. 3)
    学习资料
    鱼眼图与六面图转换(python)
  • 原文地址:https://www.cnblogs.com/lxc89/p/6410236.html
Copyright © 2011-2022 走看看