JAVA 求中文首字母(大小写)

た 入场券 2022-08-23 00:53 238阅读 0赞

从别地方淘来的代码,经测试可用

  1. public class ChinaInitial {
  2. /**
  3. * 测试代码
  4. *
  5. * @param args
  6. */
  7. public static void main(String[] args) {
  8. String str = "精英班1401";
  9. System.out.println("中文首字母:" + getPYIndexStr(str, false));
  10. //第二个参数为是否大写
  11. }
  12. /**
  13. *
  14. * 返回首字母
  15. *
  16. * @param strChinese
  17. *
  18. * @param bUpCase
  19. *
  20. * @return
  21. */
  22. public static String getPYIndexStr(String strChinese, boolean bUpCase) {
  23. try {
  24. StringBuffer buffer = new StringBuffer();
  25. byte b[] = strChinese.getBytes("GBK");// 把中文转化成byte数组
  26. for (int i = 0; i < b.length; i++) {
  27. if ((b[i] & 255) > 128) {
  28. int char1 = b[i++] & 255;
  29. char1 <<= 8;// 左移运算符用“<<”表示,是将运算符左边的对象,向左移动运算符右边指定的位数,并且在低位补零。其实,向左移n位,就相当于乘上2的n次方
  30. int chart = char1 + (b[i] & 255);
  31. buffer.append(getPYIndexChar((char) chart, bUpCase));
  32. continue;
  33. }
  34. char c = (char) b[i];
  35. if (!Character.isJavaIdentifierPart(c))// 确定指定字符是否可以是 Java
  36. // 标识符中首字符以外的部分。
  37. c = 'A';
  38. buffer.append(c);
  39. }
  40. return buffer.toString();
  41. } catch (Exception e) {
  42. System.out.println((new StringBuilder())
  43. .append("\u53D6\u4E2D\u6587\u62FC\u97F3\u6709\u9519")
  44. .append(e.getMessage()).toString());
  45. }
  46. return null;
  47. }
  48. /**
  49. *
  50. * 得到首字母
  51. *
  52. * @param strChinese
  53. *
  54. * @param bUpCase
  55. *
  56. * @return
  57. */
  58. private static char getPYIndexChar(char strChinese, boolean bUpCase) {
  59. int charGBK = strChinese;
  60. char result;
  61. if (charGBK >= 45217 && charGBK <= 45252)
  62. result = 'A';
  63. else if (charGBK >= 45253 && charGBK <= 45760)
  64. result = 'B';
  65. else if (charGBK >= 45761 && charGBK <= 46317)
  66. result = 'C';
  67. else if (charGBK >= 46318 && charGBK <= 46825)
  68. result = 'D';
  69. else if (charGBK >= 46826 && charGBK <= 47009)
  70. result = 'E';
  71. else if (charGBK >= 47010 && charGBK <= 47296)
  72. result = 'F';
  73. else if (charGBK >= 47297 && charGBK <= 47613)
  74. result = 'G';
  75. else if (charGBK >= 47614 && charGBK <= 48118)
  76. result = 'H';
  77. else if (charGBK >= 48119 && charGBK <= 49061)
  78. result = 'J';
  79. else if (charGBK >= 49062 && charGBK <= 49323)
  80. result = 'K';
  81. else if (charGBK >= 49324 && charGBK <= 49895)
  82. result = 'L';
  83. else if (charGBK >= 49896 && charGBK <= 50370)
  84. result = 'M';
  85. else if (charGBK >= 50371 && charGBK <= 50613)
  86. result = 'N';
  87. else if (charGBK >= 50614 && charGBK <= 50621)
  88. result = 'O';
  89. else if (charGBK >= 50622 && charGBK <= 50905)
  90. result = 'P';
  91. else if (charGBK >= 50906 && charGBK <= 51386)
  92. result = 'Q';
  93. else if (charGBK >= 51387 && charGBK <= 51445)
  94. result = 'R';
  95. else if (charGBK >= 51446 && charGBK <= 52217)
  96. result = 'S';
  97. else if (charGBK >= 52218 && charGBK <= 52697)
  98. result = 'T';
  99. else if (charGBK >= 52698 && charGBK <= 52979)
  100. result = 'W';
  101. else if (charGBK >= 52980 && charGBK <= 53688)
  102. result = 'X';
  103. else if (charGBK >= 53689 && charGBK <= 54480)
  104. result = 'Y';
  105. else if (charGBK >= 54481 && charGBK <= 55289)
  106. result = 'Z';
  107. else
  108. result = (char) (65 + (new Random()).nextInt(25));
  109. if (!bUpCase)
  110. result = Character.toLowerCase(result);
  111. return result;
  112. }
  113. }

发表评论

表情:
评论列表 (有 0 条评论,238人围观)

还没有评论,来说两句吧...

相关阅读