首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

java 汉语转拼音、取出首字母、转换手机按键数

2012-09-07 
java 中文转拼音、取出首字母、转换手机按键数java 中文转拼音、取出首字母、转换手机按键数:(1)、需要pinying4

java 中文转拼音、取出首字母、转换手机按键数
java 中文转拼音、取出首字母、转换手机按键数:

(1)、需要pinying4j.jar包。

(2)、代码:

import java.util.HashMap;import java.util.Map;import net.sourceforge.pinyin4j.PinyinHelper;import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;public class CnToSpell {    /**     * 将汉字转换为全拼(大写)     * @param src     * @return     */    public static String getPingYin(String src) {        char[] t1 = null;        t1 = src.toCharArray();        String[] t2 = new String[t1.length];        HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();        t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);        t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);        t3.setVCharType(HanyuPinyinVCharType.WITH_V);        String t4 = "";        int t0 = t1.length;        try {            for (int i = 0; i < t0; i++) {                // 判断是否为汉字字符                if (java.lang.Character.toString(t1[i]).matches(                        "[\\u4E00-\\u9FA5]+")) {                    t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);                    //System.out.println("---" + t2[0].toUpperCase()+"----"+t1[i]);                    //t4 += t2[0];                    t4 += t2[0].toUpperCase() + t1[i];                } else                    t4 += java.lang.Character.toString(t1[i]);            }            // System.out.println(t4);            return t4;        } catch (BadHanyuPinyinOutputFormatCombination e1) {            e1.printStackTrace();        }        return t4;    }        /**     * 返回中文的首字母     * @param str     * @return     */    public static String getPinYinHeadChar(String str) {        String temp = "";        String demo = "";        String convert = "";        for (int j = 0; j < str.length(); j++) {            char word = str.charAt(j);            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);            if (pinyinArray != null) {                convert += pinyinArray[0].charAt(0);            } else {                convert += word;            }        }        for(int i=0;i<convert.length();i++){//convert目前为小写首字母,下面是将小写首字母转化为大写            if(convert.charAt(i)>='a' && convert.charAt(i)<='z'){                temp=convert.substring(i,i+1).toUpperCase();                demo += temp;            }        }        return demo;    }        /**     * 将字符串转移为ASCII码     * @param cnStr     * @return     */    public static String getCnASCII(String cnStr) {        StringBuffer strBuf = new StringBuffer();        byte[] bGBK = cnStr.getBytes();        for (int i = 0; i < bGBK.length; i++) {            // System.out.println(Integer.toHexString(bGBK[i]&0xff));            strBuf.append(Integer.toHexString(bGBK[i] & 0xff));        }        return strBuf.toString();    }        /**     * 拼音转手机按键数     * @param cnStr     * @return     */    public static String getPinYinToKeypad(String cnStr){        char[] t1 = null;        t1 = cnStr.toCharArray();        String[] t2 = new String[t1.length];        HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();        t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);        t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);        t3.setVCharType(HanyuPinyinVCharType.WITH_V);        String t4 = "";        int t0 = t1.length;        try {            for (int i = 0; i < t0; i++) {                // 判断是否为汉字字符                if (java.lang.Character.toString(t1[i]).matches(                        "[\\u4E00-\\u9FA5]+")) {                    t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);                    String pinying = t2[0];                   // System.out.println("-----" + pinying.length());                    String num = "";                    for(int j=0;j<pinying.length();j++){                    //System.out.println("=====" + t2[0].charAt(j));                    //System.out.println("+++++" + getCnNum(String.valueOf(t2[0].charAt(j))));                    num += getCnNum(String.valueOf(t2[0].charAt(j)));                    }                    t4 += num + " ";                } else if(java.lang.Character.toString(t1[i]).matches(                "[a-zA-Z]+")){                                String pinying = java.lang.Character.toString(t1[i]).toLowerCase();                    t4 += getCnNum(String.valueOf(pinying));                                    }else{                t4 += java.lang.Character.toString(t1[i]);                }            }            return t4;        } catch (BadHanyuPinyinOutputFormatCombination e1) {            e1.printStackTrace();        }        return t4;    }        public static String getCnNum(String str){        Map map = new HashMap();    map.put("a", 2);    map.put("b", 2);    map.put("c", 2);    map.put("d", 3);    map.put("e", 3);    map.put("f", 3);    map.put("g", 4);    map.put("h", 4);    map.put("i", 4);    map.put("j", 5);    map.put("k", 5);    map.put("l", 5);    map.put("m", 6);    map.put("n", 6);    map.put("o", 6);    map.put("p", 7);    map.put("q", 7);    map.put("r", 7);    map.put("s", 7);    map.put("t", 8);    map.put("u", 8);    map.put("v", 8);    map.put("w", 9);    map.put("x", 9);    map.put("y", 9);    map.put("z", 9);        return String.valueOf(map.get(str));    }    public static void main(String[] args) {        String cnStr = "姓名";        System.out.println(getPingYin(cnStr));        System.out.println(getPinYinHeadChar(cnStr));        System.out.println(getPinYinToKeypad(cnStr));    }}
?

热点排行