按字母顺序以及区间排序汉字

来源:互联网 发布:网络渗透的目的 编辑:程序博客网 时间:2024/05/18 15:06

这个是我结合连个人的东东写的,

1.首先,先给汉字按字母顺序排序

2.获取每个单词的首字母

3,比较首字母的asc码,进行区间输出比如输出A-G

package test;


import java.text.Collator;
import java.util.Arrays;
import java.util.Comparator;


public class Test3 {
// 简体中文的编码范围从B0A1(45217)一直到F7FE(63486)  
    private static int BEGIN = 45217;  
    private static int END = 63486;  
  
    // 按照声 母表示,这个表是在GB2312中的出现的第一个汉字,也就是说“啊”是代表首字母a的第一个汉字。  
    // i, u, v都不做声母, 自定规则跟随前面的字母  
    private static char[] chartable = { '啊', '芭', '擦', '搭', '蛾', '发', '噶', '哈', '哈', '击', '喀', '垃', '妈', '拿', '哦', '啪', '期', '然', '撒', '塌', '塌', '塌', '挖', '昔', '压', '匝', };  
  
    // 二十六个字母区间对应二十七个端点  
    // GB2312码汉字区间十进制表示  
    private static int[] table = new int[27];  
  
    // 对应首字母区间表  
    private static char[] initialtable = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'h', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 't', 't', 'w', 'x', 'y', 'z', };  
  
    // 初始化  
    static {  
        for (int i = 0; i < 26; i++) {  
            table[i] = gbValue(chartable[i]);// 得到GB2312码的首字母区间端点表,十进制。  
        }  
        table[26] = END;// 区间表结尾  
    }  
    public static String cn2py(String SourceStr) {  
        String Result = "";  
        int StrLength = SourceStr.length();  
        int i;  
        try {  
            for (i = 0; i < StrLength; i++) {  
            if(i==0){
            Result += Char2Initial(SourceStr.charAt(i)); 
            }
            }  
        } catch (Exception e) {  
            Result = "";  
            e.printStackTrace();  
        }  
        return Result;  
    }  
 // ------------------------private方法区------------------------  
    /** 
     * 输入字符,得到他的声母,英文字母返回对应的大写字母,其他非简体汉字返回 '0'   *    
     */  
    private static char Char2Initial(char ch) {  
        // 对英文字母的处理:小写字母转换为大写,大写的直接返回  
        if (ch >= 'a' && ch <= 'z') {  
            return (char) (ch - 'a' + 'A');  
        }  
        if (ch >= 'A' && ch <= 'Z') {  
            return ch;  
        }  
        // 对非英文字母的处理:转化为首字母,然后判断是否在码表范围内,  
        // 若不是,则直接返回。  
        // 若是,则在码表内的进行判断。  
        int gb = gbValue(ch);// 汉字转换首字母  
        if ((gb < BEGIN) || (gb > END))// 在码表区间之前,直接返回  
        {  
            return ch;  
        }  
        int i;  
        for (i = 0; i < 26; i++) {// 判断匹配码表区间,匹配到就break,判断区间形如“[,)”  
            if ((gb >= table[i]) && (gb < table[i + 1])) {  
                break;  
            }  
        }  
        if (gb == END) {// 补上GB2312区间最右端  
            i = 25;  
        }  
        return initialtable[i]; // 在码表区间中,返回首字母  
    }  
    /** 
     * 取出汉字的编码 cn 汉字    
     */  
    private static int gbValue(char ch) {// 将一个汉字(GB2312)转换为十进制表示。  
        String str = new String();  
        str += ch;  
        try {  
            byte[] bytes = str.getBytes("GB2312");  
            if (bytes.length < 2) {  
                return 0;  
            }  
            return (bytes[0] << 8 & 0xff00) + (bytes[1] & 0xff);  
        } catch (Exception e) {  
            return 0;  
        }  
    }  
@SuppressWarnings("unchecked")
public static void main(String[] args) {
/*
*1.首先,将所有的汉字数据按照字母顺序排列
*2.将排序好的汉字获取汉字的声母
*3.根据字母的asc码进行区域排序 
*
*/
String[] list={"啊地方","吧的","吃一体化","的规划图","额房顶上","非","个","好","一","就","看","了","吗","你","哦","票","去","人","是","他","有","我","想","有","在"};
@SuppressWarnings("rawtypes")
Comparator comp = Collator.getInstance(java.util.Locale.CHINESE);  
Arrays.sort(list,comp);
for (int i = 0; i < list.length; i++) {
String word=cn2py(list[i]);
System.out.println(word);
char[] wordChar=word.toCharArray();
for (int j = 0; j < wordChar.length; j++) {
if((int)wordChar[j]<=103){
System.out.println(list[i]);
}
}
}

}
}


0 0
原创粉丝点击