• Java 输入汉字输出拼音首字母


      1 public class LetterMatchUtil {
    2 private static String _ToEncode_ = "GBK";
    3 private static String _FromEncode_ = "GBK";
    4
    5 public static int compare(String str1, String str2) {
    6 int result = 0;
    7 String m_s1 = null;
    8 String m_s2 = null;
    9 try {
    10 m_s1 = new String(str1.getBytes(_FromEncode_), _ToEncode_);
    11 m_s2 = new String(str2.getBytes(_FromEncode_), _ToEncode_);
    12 } catch (Exception e) {
    13 return str1.compareTo(str2);
    14 }
    15 result = chineseCompareTo(m_s1, m_s2);
    16 return result;
    17 }
    18
    19 public static int getCharCode(String s) {
    20 if (s == null || s.equals(""))
    21 return -1;
    22 byte b[] = s.getBytes();
    23 int value = 0;
    24 for (int i = 0; i < b.length && i <= 2; i++) {
    25 value = value * 100 + b[i];
    26 }
    27 return value;
    28 }
    29
    30 public static int chineseCompareTo(String s1, String s2) {
    31 int len1 = s1.length();
    32 int len2 = s2.length();
    33 int n = Math.min(len1, len2);
    34 for (int i = 0; i < n; i++) {
    35 int s1_code = getCharCode(s1.charAt(i) + "");
    36 int s2_code = getCharCode(s2.charAt(i) + "");
    37 if (s1_code * s2_code < 0)
    38 return Math.min(s1_code, s2_code);
    39 if (s1_code != s2_code)
    40 return s1_code - s2_code;
    41 }
    42 return len1 - len2;
    43 }
    44
    45 public static String getBeginCharacter(String res) {
    46 String a = res;
    47 String result = "";
    48 for (int i = 0; i < a.length(); i++) {
    49 String current = a.substring(i, i + 1);
    50 if (compare(current, "\u554A ") < 0)
    51 result = result + current;
    52 else if (compare(current, "\u554A ") >= 0
    53 && compare(current, "\u5EA7 ") <= 0)
    54 if (compare(current, "\u531D ") >= 0)
    55 result = result + "z";
    56 else if (compare(current, "\u538B ") >= 0)
    57 result = result + "y";
    58 else if (compare(current, "\u6614 ") >= 0)
    59 result = result + "x";
    60 else if (compare(current, "\u6316 ") >= 0)
    61 result = result + "w";
    62 else if (compare(current, "\u584C ") >= 0)
    63 result = result + "t";
    64 else if (compare(current, "\u6492 ") >= 0)
    65 result = result + "s";
    66 else if (compare(current, "\u7136 ") >= 0)
    67 result = result + "r";
    68 else if (compare(current, "\u671F ") >= 0)
    69 result = result + "q";
    70 else if (compare(current, "\u556A ") >= 0)
    71 result = result + "p";
    72 else if (compare(current, "\u54E6 ") >= 0)
    73 result = result + "o";
    74 else if (compare(current, "\u62FF ") >= 0)
    75 result = result + "n";
    76 else if (compare(current, "\u5988 ") >= 0)
    77 result = result + "m";
    78 else if (compare(current, "\u5783 ") >= 0)
    79 result = result + "l";
    80 else if (compare(current, "\u5580 ") >= 0)
    81 result = result + "k";
    82 else if (compare(current, "\u51FB ") > 0)
    83 result = result + "j";
    84 else if (compare(current, "\u54C8 ") >= 0)
    85 result = result + "h";
    86 else if (compare(current, "\u5676 ") >= 0)
    87 result = result + "g";
    88 else if (compare(current, "\u53D1 ") >= 0)
    89 result = result + "f";
    90 else if (compare(current, "\u86FE ") >= 0)
    91 result = result + "e";
    92 else if (compare(current, "\u642D ") >= 0)
    93 result = result + "d";
    94 else if (compare(current, "\u64E6 ") >= 0)
    95 result = result + "c";
    96 else if (compare(current, "\u82AD ") >= 0)
    97 result = result + "b";
    98 else if (compare(current, "\u554A ") >= 0)
    99 result = result + "a";
    100 }
    101
    102 return result;
    103 }
    104
    105 public static String getFirstStr(String str) {
    106 String result = "";
    107 char a= ' ';
    108 for(int i = 0; i < str.length(); i++){
    109 a = str.charAt(i);
    110 char aa[] = {a};
    111 String sss = new String(aa);
    112 if (Character.isDigit(aa[0]))
    113 sss = "data";
    114 else if (a >= 'a' && a <= 'z' || a >= 'A' && a <= 'Z')
    115 sss = "character";
    116 else
    117 sss = getBeginCharacter(sss);
    118 result = result + sss;
    119 }
    120 return result;
    121 }
    122
    123 public static void main(String[] args){
    124 System.out.println(getFirstStr("你是谁"));
    125 }
    126
    127 }
  • 相关阅读:
    MeshLab编译理解
    几个经典的数学库之一学习---VCGlib(1)
    开源协议的几种类型认识
    Win & Mac 系统之间U盘传递的U盘文件格式选取问题
    用VS2010编译python2.7的源码
    类之间关系理解:组合>聚合>关联>依赖;实现,继承
    Linux进阶:让效率翻倍的Bash技巧(一)
    Linux下nc命来实现文件传输
    nginx 405 not allowed问题的解决
    vim同时打开多个文件进行编辑
  • 原文地址:https://www.cnblogs.com/lihuiyy/p/2415282.html
Copyright © 2020-2023  润新知