PinyinUtil.java package com.myUtil;import java.util.ArrayList;import java.util.List;import net.sourceforge.pinyin4j.PinyinHelper;import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;import net.sourceforge.pinyin4j.format.HanyuPinyinOu
package com.myUtil; import java.util.ArrayList; import java.util.List; 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 PinyinUtil { /** * 获取中文字符串的一组全拼 * @param 中文字符串 * @return hanyupinyin汉语拼音 */ public static String getSingleFullPinyin(String cnString) { StringBuilder sb = new StringBuilder(); char[] arr = cnString.toCharArray(); for (char c : arr) { if (c > 128) { try { sb.append(PinyinHelper.toHanyuPinyinStringArray(c, outputFormat)[0]); } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } } else { sb.append(c); } } return sb.toString(); } /** * 获取中文字符串的一组首字母 * @param 中文字符串 * @return hypy汉语拼音 */ public static String getSingleFirstPinyin(String cnString) { StringBuilder sb = new StringBuilder(); char[] arr = cnString.toCharArray(); for (char c : arr) { if (c > 128) { try { sb.append(PinyinHelper.toHanyuPinyinStringArray(c, outputFormat)[0].charAt(0)); } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } } else { sb.append(c); } } return sb.toString(); } /** * 获取中文字符串的所有全拼组合(包括多音字) * @param 中文字符串 * @return hanyupinyin汉语拼音的List */ public static ListgetAllFirstPinyin(String cnString) { char[] arr = cnString.toCharArray(); String[][] strings = new String[cnString.length()][]; for (int i = 0; i < strings.length; i++) { if (arr[i] > 128) { try { String[] tmpStrings = PinyinHelper.toHanyuPinyinStringArray(arr[i], outputFormat); strings[i] = new String[tmpStrings.length]; for (int j = 0; j < tmpStrings.length; j++) { strings[i][j] = tmpStrings[j].substring(0, 1); } } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } } else { strings[i] = new String[]{String.valueOf(arr[i])}; } } List list = new ArrayList (); permuteAndCombine(list, strings, 0, 0, ""); return list; } /** * 获取中文字符串的所有首字母组合(包括多音字) * @param 中文字符串 * @return hypy汉语拼音的List */ public static List getAllFullPinyin(String cnString) { char[] arr = cnString.toCharArray(); String[][] strings = new String[cnString.length()][]; for (int i = 0; i < strings.length; i++) { if (arr[i] > 128) { try { strings[i] = PinyinHelper.toHanyuPinyinStringArray(arr[i], outputFormat); } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } } else { strings[i] = new String[]{String.valueOf(arr[i])}; } } List list = new ArrayList (); permuteAndCombine(list, strings, 0, 0, ""); return list; } /** * 二维数组排列组合 * @return 结果放到list */ private static void permuteAndCombine(List list, String[][] strings, int i, int j, String before) { if (i < strings.length - 1) { for (; j < strings[i].length; j++) { String after = before + strings[i][j]; permuteAndCombine(list, strings, i+1, 0, after); } } else { for (String str : strings[i]) { String after = before + str; if (!list.contains(after)) { list.add(after); } } } } /** * 定义拼音的输出格式 */ private static HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat(); static { //默认设置小写 outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); //默认设置没有音调 outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); //默认设置显示v outputFormat.setVCharType(HanyuPinyinVCharType.WITH_V); } public static HanyuPinyinOutputFormat getOutputFormat() { return outputFormat; } public static void setOutputFormat(HanyuPinyinOutputFormat outputFormat) { PinyinUtil.outputFormat = outputFormat; } }