zoukankan      html  css  js  c++  java
  • ChineseHelper(获取汉字字符串的首拼)

      1     public class ChineseHelper
      2     {
      3 
      4         #region 获取汉字字符串的首拼
      5         /// <summary>
      6         /// 在指定的字符串列表CnStr中检索符合拼音索引字符串
      7         /// </summary>
      8         /// <param name="CnStr">汉字字符串</param>
      9         /// <returns>相对应的汉语拼音首字母串</returns>
     10         public static string GetSpellCode(string CnStr)
     11         {
     12             string strTemp = "";
     13             int iLen = CnStr.Length;
     14             int i = 0;
     15             for (i = 0; i <= iLen - 1; i++)
     16             {
     17                 strTemp += GetCharSpellCode(CnStr.Substring(i, 1));
     18             }
     19             return strTemp;
     20         }
     21 
     22         /// <summary>
     23         /// 得到一个汉字的拼音第一个字母,如果是一个英文字母则直接返回大写字母
     24         /// </summary>
     25         /// <param name="CnChar">单个汉字</param>
     26         /// <returns>单个大写字母</returns>
     27         private static string GetCharSpellCode(string CnChar)
     28         {
     29             long iCnChar;
     30             byte[] ZW = System.Text.Encoding.Default.GetBytes(CnChar);
     31             //如果是字母,则直接返回首字母
     32             if (ZW.Length == 1)
     33             {
     34                 return CommonMethod.CutString(CnChar.ToUpper(), 1);
     35             }
     36             else
     37             {
     38                 // get the array of byte from the single char
     39                 int i1 = (short)(ZW[0]);
     40                 int i2 = (short)(ZW[1]);
     41                 iCnChar = i1 * 256 + i2;
     42             }
     43             // iCnChar match the constant
     44             #region
     45             if ((iCnChar >= 45217) && (iCnChar <= 45252))
     46             {
     47                 return "A";
     48             }
     49             else if ((iCnChar >= 45253) && (iCnChar <= 45760))
     50             {
     51                 return "B";
     52             }
     53             else if ((iCnChar >= 45761) && (iCnChar <= 46317))
     54             {
     55                 return "C";
     56             }
     57             else if ((iCnChar >= 46318) && (iCnChar <= 46825))
     58             {
     59                 return "D";
     60             }
     61             else if ((iCnChar >= 46826) && (iCnChar <= 47009))
     62             {
     63                 return "E";
     64             }
     65             else if ((iCnChar >= 47010) && (iCnChar <= 47296))
     66             {
     67                 return "F";
     68             }
     69             else if ((iCnChar >= 47297) && (iCnChar <= 47613))
     70             {
     71                 return "G";
     72             }
     73             else if ((iCnChar >= 47614) && (iCnChar <= 48118))
     74             {
     75                 return "H";
     76             }
     77             else if ((iCnChar >= 48119) && (iCnChar <= 49061))
     78             {
     79                 return "J";
     80             }
     81             else if ((iCnChar >= 49062) && (iCnChar <= 49323))
     82             {
     83                 return "K";
     84             }
     85             else if ((iCnChar >= 49324) && (iCnChar <= 49895))
     86             {
     87                 return "L";
     88             }
     89             else if ((iCnChar >= 49896) && (iCnChar <= 50370))
     90             {
     91                 return "M";
     92             }
     93             else if ((iCnChar >= 50371) && (iCnChar <= 50613))
     94             {
     95                 return "N";
     96             }
     97             else if ((iCnChar >= 50614) && (iCnChar <= 50621))
     98             {
     99                 return "O";
    100             }
    101             else if ((iCnChar >= 50622) && (iCnChar <= 50905))
    102             {
    103                 return "P";
    104             }
    105             else if ((iCnChar >= 50906) && (iCnChar <= 51386))
    106             {
    107                 return "Q";
    108             }
    109             else if ((iCnChar >= 51387) && (iCnChar <= 51445))
    110             {
    111                 return "R";
    112             }
    113             else if ((iCnChar >= 51446) && (iCnChar <= 52217))
    114             {
    115                 return "S";
    116             }
    117             else if ((iCnChar >= 52218) && (iCnChar <= 52697))
    118             {
    119                 return "T";
    120             }
    121             else if ((iCnChar >= 52698) && (iCnChar <= 52979))
    122             {
    123                 return "W";
    124             }
    125             else if ((iCnChar >= 52980) && (iCnChar <= 53640))
    126             {
    127                 return "X";
    128             }
    129             else if ((iCnChar >= 53689) && (iCnChar <= 54480))
    130             {
    131                 return "Y";
    132             }
    133             else if ((iCnChar >= 54481) && (iCnChar <= 55289))
    134             {
    135                 return "Z";
    136             }
    137             else
    138                 return ("-");
    139             #endregion
    140         }
    141 
    142         #endregion
    143     }
    144     public class CommonMethod
    145     {
    146         #region 截取字符长度 static string CutString(string str, int len)
    147         /// <summary>
    148         /// 截取字符长度
    149         /// </summary>
    150         /// <param name="str">被截取的字符串</param>
    151         /// <param name="len">所截取的长度</param>
    152         /// <returns>子字符串</returns>
    153         public static string CutString(string str, int len)
    154         {
    155             if (str == null || str.Length == 0 || len <= 0)
    156             {
    157                 return string.Empty;
    158             }
    159 
    160             int l = str.Length;
    161 
    162 
    163             #region 计算长度
    164             int clen = 0;
    165             while (clen < len && clen < l)
    166             {
    167                 //每遇到一个中文,则将目标长度减一。
    168                 if ((int)str[clen] > 128) { len--; }
    169                 clen++;
    170             }
    171             #endregion
    172 
    173             if (clen < l)
    174             {
    175                 return str.Substring(0, clen) + "...";
    176             }
    177             else
    178             {
    179                 return str;
    180             }
    181         }
    182         #endregion
    183     }
  • 相关阅读:
    FlashSocke 通过flash进行socket通信(as代码)
    JavaScript 中的对象深度复制(Object Deep Clone)
    map,vector 等容器内容的循环删除问题(C++)
    [转]用JavaScript在浏览器中创建下载文件
    [记]WIndow/Linux 获取本机(全部)IPv4、IPv6、MAC地址方法 (C/C++)
    [记]Debian alias 设置, 不设置貌似有点不方便习惯
    Linux 安装配置 FTP 服务 (vsftpd)
    FreeSWITCH 安装配置的 各种坑, 填坑
    ubuntu编译安装ruby1.9.3,从p551降级到p484
    redmine3.3.3 rake db:migrate 报错invalid byte sequence in US-ASCII (Argument Error) 解决方法
  • 原文地址:https://www.cnblogs.com/luoshengjie/p/11097208.html
Copyright © 2011-2022 走看看