关于汉字转拼音

由于android 的sqlite无法对中文排序,所以得把汉字转成拼音进度排序。

 

转换工具:pinyin4j-2.5.0.jar

 

性能:速度比较慢,100条三个字的数据大概2000ms

 

封装代码:

public static String toPinYin(String hanzhis) {
        CharSequence s = hanzhis;

        char[] hanzhi = new char[s.length()];
        for (int i = 0; i < s.length(); i++) {
            hanzhi[i] = s.charAt(i);
        }

        char[] t1 = hanzhi;
        String[] t2 = new String[s.length()];
        /** */
        /**
         * 设置输出格式
         */
        net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
        t3.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        t3.setVCharType(HanyuPinyinVCharType.WITH_V);

        int t0 = t1.length;
        String py = "";
        try {
            for (int i = 0; i < t0; i++) {
                t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
                py = py + t2[0].toString();
            }
        } catch (BadHanyuPinyinOutputFormatCombination e1) {
            e1.printStackTrace();
        }

        return py.trim();
    }

public static String toPinYin(String hanzhis) {
		CharSequence s = hanzhis;

		char[] hanzhi = new char[s.length()];
		for (int i = 0; i < s.length(); i++) {
			hanzhi[i] = s.charAt(i);
		}

		char[] t1 = hanzhi;
		String[] t2 = new String[s.length()];
		/** */
		/**
		 * 设置输出格式
		 */
		net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
		t3.setCaseType(HanyuPinyinCaseType.UPPERCASE);
		t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		t3.setVCharType(HanyuPinyinVCharType.WITH_V);

		int t0 = t1.length;
		String py = "";
		try {
			for (int i = 0; i < t0; i++) {
				t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
				py = py + t2[0].toString();
			}
		} catch (BadHanyuPinyinOutputFormatCombination e1) {
			e1.printStackTrace();
		}

		return py.trim();
	}
 

你可能感兴趣的:(汉字)