汉字问题【比较】

不知是那个大侠的东东,不错啊~~

public class Letter {
  private static String _FromEncode_ = "GBK";
  private static String _ToEncode_ = "GBK";
  public  Letter() {
  }

  public static int compare(String str1,String str2){
    int result =0;
    String m_s1 = null,m_s2 = null;
    try {
      m_s1 = new String(str1.getBytes(_FromEncode_),_ToEncode_);
      m_s2 = new String(str2.getBytes(_FromEncode_),_ToEncode_);
    } catch(Exception e){
      return str1.compareTo(str2);
    }
    result = chineseCompareTo(m_s1,m_s2);
    return result;
  }

  public  static int getCharCode(String s){
    if (s == null && s.equals(""))
      return -1;
    byte[] b = s.getBytes();
    int value = 0;
    for (int i = 0; i < b.length && i <= 2; i++){
      value = value*100 + b[i]; //*100左移
    }
    return value;
  }

  public static int chineseCompareTo(String s1, String s2) {
    int len1 = s1.length();
    int len2 = s2.length();
    int n = Math.min(len1, len2);
    for (int i = 0; i < n; i++) {
      int s1_code = getCharCode(s1.charAt(i) + "");
      int s2_code = getCharCode(s2.charAt(i) + "");
      if (s1_code*s2_code<0)
        return Math.min(s1_code,s2_code);
      if (s1_code != s2_code)
        return s1_code - s2_code;
    }
    return len1 - len2;
  }


  public static String getBeginCharacter(String res) {
    String a = res;
    String result = "";
    String current;

    for(int i=0;i<a.length();i++){
      current = a.substring(i,i+1);
      if (Letter.compare(current,"啊")<0 ) //字母,数字原样返回
        result += current;
      else if ((Letter.compare(current, "啊")>=0) && (Letter.compare(current,"座")) <= 0){
        if (Letter.compare(current,"匝") >= 0)
          result += "z";
        else if (Letter.compare(current,"压") >=0)
          result += "y";
        else if (Letter.compare(current,"昔") >=0)
          result += "x";
        else if (Letter.compare(current,"挖") >=0)
          result += "w";
        else if (Letter.compare(current,"塌") >=0)
          result += "t";
        else if (Letter.compare(current,"撒") >=0)
          result += "s";
        else if (Letter.compare(current,"然") >=0)
          result += "r";
        else if (Letter.compare(current,"期") >=0)
          result += "q";
        else if (Letter.compare(current,"啪") >=0)
          result += "p";
        else if (Letter.compare(current,"哦") >=0)
          result += "o";
        else if (Letter.compare(current,"拿") >=0)
          result += "n";
        else if (Letter.compare(current,"妈") >=0)
          result += "m";
        else if (Letter.compare(current,"垃") >=0)
          result += "l";
        else if (Letter.compare(current,"喀") >=0)
          result += "k";
        else if (Letter.compare(current,"击") >0)
          result += "j";
        else if (Letter.compare(current,"哈") >=0)
          result += "h";
        else if (Letter.compare(current,"噶") >=0)
          result += "g";
        else if (Letter.compare(current,"发") >=0)
          result += "f";
        else if (Letter.compare(current,"蛾") >=0)
          result += "e";
        else if (Letter.compare(current,"搭") >=0)
          result += "d";
        else if (Letter.compare(current,"擦") >=0)
          result += "c";
        else if (Letter.compare(current,"芭") >=0)
          result += "b";
        else if (Letter.compare(current,"啊") >=0)
          result += "a";
      }
    }
    return result.toUpperCase();
  }
 
  public static void main(String[] args){
   Letter lt=new Letter();
   if(lt.getBeginCharacter("测试").Equals("CS"))System.out.println("Successed!");
  }

}

 

 

阅读更多
个人分类: java_j2se
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭