⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cn2spellutil.java

📁 java环境中汉字自动转为拼音算法,拼音为还可以为首字母
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        spellPut("qu", -14345);    
        spellPut("quan", -14170);    
        spellPut("que", -14159);    
        spellPut("qun", -14151);    
        spellPut("ran", -14149);    
        spellPut("rang", -14145);    
        spellPut("rao", -14140);    
        spellPut("re", -14137);    
        spellPut("ren", -14135);    
        spellPut("reng", -14125);    
        spellPut("ri", -14123);    
        spellPut("rong", -14122);    
        spellPut("rou", -14112);    
        spellPut("ru", -14109);    
        spellPut("ruan", -14099);    
        spellPut("rui", -14097);    
        spellPut("run", -14094);    
        spellPut("ruo", -14092);    
        spellPut("sa", -14090);    
        spellPut("sai", -14087);    
        spellPut("san", -14083);    
        spellPut("sang", -13917);    
        spellPut("sao", -13914);    
        spellPut("se", -13910);    
        spellPut("sen", -13907);    
        spellPut("seng", -13906);    
        spellPut("sha", -13905);    
        spellPut("shai", -13896);    
        spellPut("shan", -13894);    
        spellPut("shang", -13878);    
        spellPut("shao", -13870);    
        spellPut("she", -13859);    
        spellPut("shen", -13847);    
        spellPut("sheng", -13831);    
        spellPut("shi", -13658);    
        spellPut("shou", -13611);    
        spellPut("shu", -13601);    
        spellPut("shua", -13406);    
        spellPut("shuai", -13404);    
        spellPut("shuan", -13400);    
        spellPut("shuang", -13398);    
        spellPut("shui", -13395);    
        spellPut("shun", -13391);    
        spellPut("shuo", -13387);    
        spellPut("si", -13383);    
        spellPut("song", -13367);    
        spellPut("sou", -13359);    
        spellPut("su", -13356);    
        spellPut("suan", -13343);    
        spellPut("sui", -13340);    
        spellPut("sun", -13329);    
        spellPut("suo", -13326);    
        spellPut("ta", -13318);    
        spellPut("tai", -13147);    
        spellPut("tan", -13138);    
        spellPut("tang", -13120);    
        spellPut("tao", -13107);    
        spellPut("te", -13096);    
        spellPut("teng", -13095);    
        spellPut("ti", -13091);    
        spellPut("tian", -13076);    
        spellPut("tiao", -13068);    
        spellPut("tie", -13063);    
        spellPut("ting", -13060);    
        spellPut("tong", -12888);    
        spellPut("tou", -12875);    
        spellPut("tu", -12871);    
        spellPut("tuan", -12860);    
        spellPut("tui", -12858);    
        spellPut("tun", -12852);    
        spellPut("tuo", -12849);    
        spellPut("wa", -12838);    
        spellPut("wai", -12831);    
        spellPut("wan", -12829);    
        spellPut("wang", -12812);    
        spellPut("wei", -12802);    
        spellPut("wen", -12607);    
        spellPut("weng", -12597);    
        spellPut("wo", -12594);    
        spellPut("wu", -12585);    
        spellPut("xi", -12556);    
        spellPut("xia", -12359);    
        spellPut("xian", -12346);    
        spellPut("xiang", -12320);    
        spellPut("xiao", -12300);    
        spellPut("xie", -12120);    
        spellPut("xin", -12099);    
        spellPut("xing", -12089);    
        spellPut("xiong", -12074);    
        spellPut("xiu", -12067);    
        spellPut("xu", -12058);    
        spellPut("xuan", -12039);    
        spellPut("xue", -11867);    
        spellPut("xun", -11861);    
        spellPut("ya", -11847);    
        spellPut("yan", -11831);    
        spellPut("yang", -11798);    
        spellPut("yao", -11781);    
        spellPut("ye", -11604);    
        spellPut("yi", -11589);    
        spellPut("yin", -11536);    
        spellPut("ying", -11358);    
        spellPut("yo", -11340);    
        spellPut("yong", -11339);    
        spellPut("you", -11324);    
        spellPut("yu", -11303);    
        spellPut("yuan", -11097);    
        spellPut("yue", -11077);    
        spellPut("yun", -11067);    
        spellPut("za", -11055);    
        spellPut("zai", -11052);    
        spellPut("zan", -11045);    
        spellPut("zang", -11041);    
        spellPut("zao", -11038);    
        spellPut("ze", -11024);    
        spellPut("zei", -11020);    
        spellPut("zen", -11019);    
        spellPut("zeng", -11018);    
        spellPut("zha", -11014);    
        spellPut("zhai", -10838);    
        spellPut("zhan", -10832);    
        spellPut("zhang", -10815);    
        spellPut("zhao", -10800);    
        spellPut("zhe", -10790);    
        spellPut("zhen", -10780);    
        spellPut("zheng", -10764);    
        spellPut("zhi", -10587);    
        spellPut("zhong", -10544);    
        spellPut("zhou", -10533);    
        spellPut("zhu", -10519);    
        spellPut("zhua", -10331);    
        spellPut("zhuai", -10329);    
        spellPut("zhuan", -10328);    
        spellPut("zhuang", -10322);    
        spellPut("zhui", -10315);    
        spellPut("zhun", -10309);    
        spellPut("zhuo", -10307);    
        spellPut("zi", -10296);    
        spellPut("zong", -10281);    
        spellPut("zou", -10274);    
        spellPut("zu", -10270);    
        spellPut("zuan", -10262);    
        spellPut("zui", -10260);    
        spellPut("zun", -10256);    
        spellPut("zuo", -10254);    
    }    
   
    /**   
     * 获得单个汉字的Ascii.    
     * @param cn char 汉字字符   
     * @return int 错误返回 0,否则返回ascii   
     */   
    private static int getCnAscii(char cn) {    
        byte[] bytes = (String.valueOf(cn)).getBytes();    
        if (bytes == null || bytes.length > 2 || bytes.length <= 0) { // 错误    
            return 0;    
        }    
        if (bytes.length == 1) { // 英文字符    
            return bytes[0];    
        }    
        if (bytes.length == 2) { // 中文字符    
            int hightByte = 256 + bytes[0];    
            int lowByte = 256 + bytes[1];    
            int ascii = (256 * hightByte + lowByte) - 256 * 256;     
            return ascii;    
        }     
        return 0; // 错误    
    }    
   
    /**   
     * 根据ASCII码到SpellMap中查找对应的拼音   
     * @param ascii int 字符对应的ASCII   
     * @return String 拼音,首先判断ASCII是否>0&<160,如果是返回对应的字符,    
     * 否则到SpellMap中查找,如果没有找到拼音,则返回null,如果找到则返回拼音.   
     */   
    private static String getSpellByAscii(int ascii) {    
        if (ascii > 0 && ascii < 160) { // 单字符--英文或半角字符    
            return String.valueOf((char) ascii);    
        }    
        if (ascii < -20319 || ascii > -10247) { // 不知道的字符    
            return null;    
        }    
        Set keySet = spellMap.keySet();    
        Iterator it = keySet.iterator();    
        String spell0 = null;    
        String spell = null;    
   
        int asciiRang0 = -20319;    
        int asciiRang;    
        while (it.hasNext()) {    
            spell = (String) it.next();    
            Object valObj = spellMap.get(spell);    
            if (valObj instanceof Integer) {    
                asciiRang = ((Integer) valObj).intValue();    
                if (ascii >= asciiRang0 && ascii < asciiRang) { // 区间找到    
                    return (spell0 == null) ? spell : spell0;    
                } else {    
                    spell0 = spell;    
                    asciiRang0 = asciiRang;    
                }    
            }    
        }    
        return null;    
    }    
   
    /**   
     * 检验字符传是否正确   
     * @param cnStr   
     * @return   
     */   
    private static boolean validate(String cnStr){    
        if (cnStr == null || cnStr.trim().equals("")) {    
            return false;    
        }else{    
            return true;    
        }    
    }    
    /**   
     * 返回字符串的全拼,是汉字转化为全拼,其它字符不进行转换   
     * @param cnStr  String 字符串   
     * @return String 转换成全拼后的字符串   
     */   
    public static String getFullSpell(String cnStr) {    
        if(!validate(cnStr)){    
            return cnStr;    
        }    
        char[] chars = cnStr.toCharArray();    
        StringBuffer retuBuf = new StringBuffer();    
        for (int i = 0, Len = chars.length; i < Len; i++) {    
            int ascii = getCnAscii(chars[i]);//得到单个中文的Ascii码    
            if (ascii == 0) { // 取ascii时出错    
                retuBuf.append(chars[i]);    
            } else {    
                String spell = getSpellByAscii(ascii);    
                if (spell == null) {    
                    retuBuf.append(chars[i]);    
                } else {    
                    retuBuf.append(spell);    
                } // end of if spell == null    
            } // end of if ascii <= -20400    
        } // end of for    
        return retuBuf.toString();    
    }    
   
    /**   
     * 返回字符串拼音的首字母,是汉字转化为拼音,其它字符不进行转换   
     * @param cnStr  String 字符串   
     * @return String 转换成拼音后的字符串(全大写)   
     */   
    public static String getFirstSpell(String cnStr) {    
        if(!validate(cnStr)){    
            return cnStr;    
        }    
        char[] chars = cnStr.toCharArray();    
        StringBuffer retuBuf = new StringBuffer();    
        for (int i = 0, Len = chars.length; i < Len; i++) {    
            int ascii = getCnAscii(chars[i]);//得到单个中文的Ascii码    
            if (ascii == 0) { // 取ascii时出错    
                retuBuf.append(chars[i]);    
            } else {    
                String spell = getSpellByAscii(ascii);    
                if (spell == null) {    
                    retuBuf.append(chars[i]);    
                } else {    
                    retuBuf.append(spell.substring(0,1));    
                } // end of if spell == null    
            } // end of if ascii <= -20400    
        } // end of for    
        return retuBuf.toString().toUpperCase();    
    }    
}   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -