获得汉字小写首字母

package com.paotui.util;

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 PinyinUtil {
	public static String converterToFirstSpell(String chines){             
		String pinyinName = "";      
		char[] nameChar = chines.toCharArray();      
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();      
		defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);      
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);      
		try {      
			pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[0], defaultFormat)[0].charAt(0);      
		} catch (BadHanyuPinyinOutputFormatCombination e) {      
			e.printStackTrace();      
		}      
		return pinyinName;      
	}    
	public static void main(String[] args) {
		String name="单宝宝";
		System.out.println(converterToFirstSpell(name));
		
	}
	
	/**
     * 得到中文首字母 ling
     * 
     * @param str
     * @return
     */
    public static String getPinYinHeadChar(String str) {

        String convert = "";
        for (int j = 0; j < 1; j++) {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert += pinyinArray[0].charAt(0);
            } else {
                convert += word;
            }
        }
        return convert;
    }
}

jar包:pinyin4j-2.5.0.jar

你可能感兴趣的:(java,汉字转拼音)