下面的代码是获取中文拼音的java类,里面有多个方法,也有获取中文拼音首字母等方法。
真是把压箱底的代码都分享出来了
package org.houyong.test.util; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; 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; import org.apache.commons.lang.StringUtils; public class pinyin4j { /** * 字符串集合转换字符串(逗号分隔) * @author wyh * @param stringSet * @return */ public static String makeStringByStringSet(Set<String> stringSet){ StringBuilder str = new StringBuilder(); int i=0; for(String s : stringSet){ if(i == stringSet.size() - 1){ str.append(s); }else{ str.append(s + ","); } i++; } return str.toString().toLowerCase(); } private static Set doubleSMSet = new HashSet(); //双声母列表 static { doubleSMSet.add("zh"); doubleSMSet.add("ch"); doubleSMSet.add("sh"); } /** * 获取拼音集合 * @author wyh * @param src * @return Set<String> */ public static Set<String> getPinyin(String src){ if(src!=null && !src.trim().equalsIgnoreCase("")){ char[] srcChar ; srcChar=src.toCharArray(); //汉语拼音格式输出类 HanyuPinyinOutputFormat hanYuPinOutputFormat = new HanyuPinyinOutputFormat(); //输出设置,大小写,音标方式等 hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); hanYuPinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); hanYuPinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V); String[][] temp = new String[src.length()][]; for(int i=0;i<srcChar.length;i++){ char c = srcChar; //是中文或者a-z或者A-Z转换拼音(我的需求,是保留中文或者a-z或者A-Z) if(String.valueOf(c).matches("[\\u4E00-\\u9FA5]+")){ try{ temp = PinyinHelper.toHanyuPinyinStringArray(srcChar, hanYuPinOutputFormat); }catch(BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } }else if(((int)c>=65 && (int)c<=90) || ((int)c>=97 && (int)c<=122) ){ temp = new String[]{String.valueOf(srcChar)}; }else{ temp = new String[]{""}; } } String[] pingyinArray = Exchange(temp); Set<String> pinyinSet = new HashSet<String>(); for(int i=0;i<pingyinArray.length;i++){ pinyinSet.add(pingyinArray); } return pinyinSet; } return null; } /* * 得到拼音,不考虑多音的情况,用每个字的第一个拼音组合成一个列表返回 */ public static List getPinyinList(String src){ if(!StringUtils.isBlank(src)){ char[] srcChar ; srcChar=src.toCharArray(); //汉语拼音格式输出类 HanyuPinyinOutputFormat hanYuPinOutputFormat = new HanyuPinyinOutputFormat(); //输出设置,大小写,音标方式等 hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); hanYuPinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); //不要声调 hanYuPinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V); StringBuffer sb = new StringBuffer(); ArrayList rtnList = new ArrayList<String>(); for(int i=0;i<srcChar.length;i++){ char c = srcChar; if(String.valueOf(c).matches("[\\u4E00-\\u9FA5]+")){ try{ String[] rtn = PinyinHelper.toHanyuPinyinStringArray(srcChar, hanYuPinOutputFormat); sb.append(rtn[0]); rtnList.add(rtn[0]); }catch(BadHanyuPinyinOutputFormatCombination e) { } }else if(((int)c>=65 && (int)c<=90) || ((int)c>=97 && (int)c<=122) || ((int)c>=48 && (int)c<=57)){ sb.append(String.valueOf(srcChar)); rtnList.add(String.valueOf(srcChar)); } } return rtnList; } return null; } public static List getPinyinFirstChList(String src){ List pyList = getPinyinList(src); List rtnList = new ArrayList<String>(); for (Iterator iterator = pyList.iterator(); iterator.hasNext();) { String sPy = (String)iterator.next(); if (sPy.length()>1 && doubleSMSet.contains(sPy.substring(0,2)) ){ rtnList.add(sPy.substring(0,2)); } else rtnList.add(sPy.substring(0,1)); } return rtnList; } /* * 得到拼音,不考虑多音的情况,用每个字的第一个拼音组合返回 */ public static String getPinyin1(String src){ if(!StringUtils.isBlank(src)){ char[] srcChar ; srcChar=src.toCharArray(); //汉语拼音格式输出类 HanyuPinyinOutputFormat hanYuPinOutputFormat = new HanyuPinyinOutputFormat(); //输出设置,大小写,音标方式等 hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); hanYuPinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); //不要声调 hanYuPinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V); StringBuffer sb = new StringBuffer(); for(int i=0;i<srcChar.length;i++){ char c = srcChar; if(String.valueOf(c).matches("[\\u4E00-\\u9FA5]+")){ try{ String[] rtn = PinyinHelper.toHanyuPinyinStringArray(srcChar, hanYuPinOutputFormat); sb.append(rtn[0]); }catch(BadHanyuPinyinOutputFormatCombination e) { } }else if(((int)c>=65 && (int)c<=90) || ((int)c>=97 && (int)c<=122) || ((int)c>=48 && (int)c<=57)){ sb.append(String.valueOf(srcChar)); } } return sb.toString(); } return null; } /** * 递归 * @author wyh * @param strJaggedArray * @return */ public static String[] Exchange(String[][] strJaggedArray){ String[][] temp = DoExchange(strJaggedArray); return temp[0]; } /** * 递归 * @author wyh * @param strJaggedArray * @return */ private static String[][] DoExchange(String[][] strJaggedArray){ int len = strJaggedArray.length; if(len >= 2){ int len1 = strJaggedArray[0].length; int len2 = strJaggedArray[1].length; int newlen = len1*len2; String[] temp = new String[newlen]; int Index = 0; for(int i=0;i<len1;i++){ for(int j=0;j<len2;j++){ temp[Index] = strJaggedArray[0] + strJaggedArray[1][j]; Index ++; } } String[][] newArray = new String[len-1][]; for(int i=2;i<len;i++){ newArray[i-1] = strJaggedArray; } newArray[0] = temp; return DoExchange(newArray); }else{ return strJaggedArray; } } /** *测试代码 * @param args */ public static void main(String[] args) { String str = "张三5"; // // String sPy =getPinyin1(str); // System.out.println("sPy="+sPy); Set sPy2 =getPinyin(str); System.out.println("sPy2="+sPy2); List pyList = getPinyinList(str); System.out.println(pyList.toString()); List pyFCList = getPinyinFirstChList(str); System.out.println(pyFCList.toString()); // // System.out.println(makeStringByStringSet(sPy)); // // System.out.println( getPinyin1(str)); String [] tmp = PinyinHelper.toGwoyeuRomatzyhStringArray(张); System.out.println(); String [] tmp1 = PinyinHelper.toHanyuPinyinStringArray(重); System.out.println(tmp1.length); } }