ChineseHelper.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace CZFW.Core.Helper
  5. {
  6. public class ChineseHelper
  7. {
  8. /// <summary>
  9. /// 在指定的字符串列表CnStr中检索符合拼音索引字符串
  10. /// </summary>
  11. /// <param name="CnStr">汉字字符串</param>
  12. /// <returns>相对应的汉语拼音首字母串</returns>
  13. public static string GetSpellCode(string CnStr, bool removeSpecialCharacter=true)
  14. {
  15. if (removeSpecialCharacter)
  16. {
  17. CnStr = CnStr.Replace("有限", "").Replace("公司", "").Replace("(", "").Replace(")", "").Replace("(", "").Replace(")", "").Replace("北京","");
  18. }
  19. string strTemp = "";
  20. int iLen = CnStr.Length;
  21. int i = 0;
  22. for (i = 0; i <= iLen - 1; i++)
  23. {
  24. strTemp += GetCharSpellCode(CnStr.Substring(i, 1));
  25. }
  26. return strTemp;
  27. }
  28. /// <summary>
  29. /// 得到一个汉字的拼音第一个字母,如果是一个英文字母则直接返回大写字母
  30. /// </summary>
  31. /// <param name="CnChar">单个汉字</param>
  32. /// <returns>单个大写字母</returns>
  33. private static string GetCharSpellCode(string CnChar)
  34. {
  35. long iCnChar;
  36. byte[] ZW = System.Text.Encoding.GetEncoding("GB2312").GetBytes(CnChar);
  37. //如果是字母,则直接返回
  38. if (ZW.Length == 1)
  39. {
  40. return CnChar.ToUpper();
  41. }
  42. else
  43. {
  44. // get the array of byte from the single char
  45. int i1 = (short)(ZW[0]);
  46. int i2 = (short)(ZW[1]);
  47. //int i3 = (short)(ZW[2]);
  48. iCnChar = i1 * 256 + i2;
  49. }
  50. // iCnChar match the constant
  51. if ((iCnChar >= 45217) && (iCnChar <= 45252))
  52. {
  53. return "A";
  54. }
  55. else if ((iCnChar >= 45253) && (iCnChar <= 45760))
  56. {
  57. return "B";
  58. }
  59. else if ((iCnChar >= 45761) && (iCnChar <= 46317))
  60. {
  61. return "C";
  62. }
  63. else if ((iCnChar >= 46318) && (iCnChar <= 46825))
  64. {
  65. return "D";
  66. }
  67. else if ((iCnChar >= 46826) && (iCnChar <= 47009))
  68. {
  69. return "E";
  70. }
  71. else if ((iCnChar >= 47010) && (iCnChar <= 47296))
  72. {
  73. return "F";
  74. }
  75. else if ((iCnChar >= 47297) && (iCnChar <= 47613))
  76. {
  77. return "G";
  78. }
  79. else if ((iCnChar >= 47614) && (iCnChar <= 48118))
  80. {
  81. return "H";
  82. }
  83. else if ((iCnChar >= 48119) && (iCnChar <= 49061))
  84. {
  85. return "J";
  86. }
  87. else if ((iCnChar >= 49062) && (iCnChar <= 49323))
  88. {
  89. return "K";
  90. }
  91. else if ((iCnChar >= 49324) && (iCnChar <= 49895))
  92. {
  93. return "L";
  94. }
  95. else if ((iCnChar >= 49896) && (iCnChar <= 50370))
  96. {
  97. return "M";
  98. }
  99. else if ((iCnChar >= 50371) && (iCnChar <= 50613))
  100. {
  101. return "N";
  102. }
  103. else if ((iCnChar >= 50614) && (iCnChar <= 50621))
  104. {
  105. return "O";
  106. }
  107. else if ((iCnChar >= 50622) && (iCnChar <= 50905))
  108. {
  109. return "P";
  110. }
  111. else if ((iCnChar >= 50906) && (iCnChar <= 51386))
  112. {
  113. return "Q";
  114. }
  115. else if ((iCnChar >= 51387) && (iCnChar <= 51445))
  116. {
  117. return "R";
  118. }
  119. else if ((iCnChar >= 51446) && (iCnChar <= 52217))
  120. {
  121. return "S";
  122. }
  123. else if ((iCnChar >= 52218) && (iCnChar <= 52697))
  124. {
  125. return "T";
  126. }
  127. else if ((iCnChar >= 52698) && (iCnChar <= 52979))
  128. {
  129. return "W";
  130. }
  131. else if ((iCnChar >= 52980) && (iCnChar <= 53640))
  132. {
  133. return "X";
  134. }
  135. else if ((iCnChar >= 53689) && (iCnChar <= 54480))
  136. {
  137. return "Y";
  138. }
  139. else if ((iCnChar >= 54481) && (iCnChar <= 55289))
  140. {
  141. return "Z";
  142. }
  143. else
  144. return ("");
  145. }
  146. }
  147. }