All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.founder.core.utils.PinYinUtils Maven / Gradle / Ivy

There is a newer version: 3.6.1.9
Show newest version
package com.founder.core.utils;

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.exception.BadHanyuPinyinOutputFormatCombination;

public class PinYinUtils {

    public static String converterToSpell(String chinese, String firstChar) {
        String pinyinName = "";
        char[] nameChar = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < chinese.length(); i++) {
            String s = String.valueOf(nameChar[i]);
            if (firstChar.matches("[\\u4e00-\\u9fa5]")) {
                try {
                    if (firstChar.equals("1")) {
                        pinyinName += PinyinHelper.toHanyuPinyinStringArray(
                                nameChar[i], defaultFormat)[0].charAt(0);
                    } else {
                        pinyinName += PinyinHelper.toHanyuPinyinStringArray(
                                nameChar[i], defaultFormat)[0];
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else if (s.matches("[a-z]") || s.matches("[A-Z]") || s.matches("[0-9]")) {
                pinyinName += s;
            }
        }
        return pinyinName.toLowerCase();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy