先把我demo贴出来,http://pan.baidu.com/s/1gd2jsAn
添加的pinyin4j包,这边有他的下载地址http://pan.baidu.com/s/1mgnMdsc
public class Change {
private String[] pinyin = null; HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); public String getCharacterPinyin(char c) { try { pinyin = PinyinHelper.toHanyuPinyinStringArray(c, format); } catch (BadHanyuPinyinOutputFormatCombination e) { // TODO Auto-generated catch block e.printStackTrace(); } if (pinyin == null) { return null; } return pinyin[0]; } public String getStringPinYin(String str) { format.setCaseType(HanyuPinyinCaseType.LOWERCASE); format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK); format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE); StringBuilder sb = new StringBuilder(); String tempPinyin = null; for (int i = 0; i < str.length(); i++) { tempPinyin = getCharacterPinyin(str.charAt(i)); if (tempPinyin == null) { sb.append(str.charAt(i)); } else { sb.append(tempPinyin); } } return sb.toString(); } }
下面是基础知识,我转载的
使用pinyin4j - 小米米儿小的个人空间 - 开源中国社区
Pinyin4j就是为了解决类似这样的问题的。它是sourceforge.net上的一个开源项目,功能非常强大:
+ 支持同一汉字有多个发音
+ 还支持拼音的格式化输出,比如第几声之类的,
基本用法
通常情况下,只需要用到其中的PinyinHelper类中的静态方法toHanyuPinyinStringArray就可以了
1 2 3 4 | String[] pinyinArray =PinyinHelper.toHanyuPinyinStringArray('单'); for(inti = 0; i < pinyinArray.length; ++i){ System.out.println(pinyinArray[i]); } |
1 2 3 4 5 6 7 8 9 10 11 12 | HanyuPinyinOutputFormat format= newHanyuPinyinOutputFormat(); format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK); format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE); String[] pinyinArray = null; try{ pinyinArray = PinyinHelper.toHanyuPinyinStringArray('黄', format); }catch(BadHanyuPinyinOutputFormatCombination e){ e.printStackTrace(); } for(inti = 0; i < pinyinArray.length; ++i){ System.out.println(pinyinArray[i]); } |
一个工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | packagecn.heimar.common.util; importjava.util.HashSet; importjava.util.Set; importnet.sourceforge.pinyin4j.PinyinHelper; importnet.sourceforge.pinyin4j.format.HanyuPinyinCaseType; importnet.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; importnet.sourceforge.pinyin4j.format.HanyuPinyinToneType; importnet.sourceforge.pinyin4j.format.HanyuPinyinVCharType; importnet.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; publicclassPinyin4jUtil { /** * 返回拼音类型:全拼 */ publicstaticfinalString RET_PINYIN_TYPE_FULL = "full"; /** * 返回拼音类型:首字母 */ publicstaticfinalString RET_PINYIN_TYPE_HEADCHAR = "headChar"; /** * 字符串集合转换字符串(逗号分隔) * * @param stringSet * @return */ publicstaticString makeStringByStringSet(Set<String> stringSet) { StringBuilder str = newStringBuilder(); inti = 0; for(String s : stringSet) { if(i == stringSet.size() - 1) { str.append(s); } else{ str.append(s + ","); } i++; } returnstr.toString().toLowerCase(); } publicstaticSet<String> str2Pinyin(String src) { returnstr2Pinyin(src, null); } /** * 字符串转换为拼音 * @param src 需要转换的字符串 * @param retType 返回拼音结果类型 * @return 如果retType为RET_PINYIN_TYPE_FULL,则返回全拼;如果retType为RET_PINYIN_TYPE_HEADCHAR;如果传入其他值,返回全拼 * @throws BadHanyuPinyinOutputFormatCombination */ publicstaticSet<String> str2Pinyin(String src, String retType) { if(src != null&& !src.trim().equalsIgnoreCase("")) { char[] srcChar; srcChar = src.toCharArray(); // 汉语拼音格式输出类 HanyuPinyinOutputFormat hanYuPinOutputFormat = newHanyuPinyinOutputFormat(); // 输出设置,大小写,音标方式等 hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); hanYuPinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); hanYuPinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V); String[][] temp = newString[src.length()][]; for(inti = 0; i < srcChar.length; i++) { charc = srcChar[i]; try{ temp[i] = PinyinHelper.toHanyuPinyinStringArray( srcChar[i], hanYuPinOutputFormat); if(temp[i] == null){//如果str.charAt(i)非汉字,则保持原样 temp[i] = newString[] { String.valueOf(srcChar[i]) }; }else{ //如果retType是RET_PINYIN_TYPE_HEADCHAR,则只取转换后的首字母 if(RET_PINYIN_TYPE_HEADCHAR.equalsIgnoreCase(retType)){ String[] temptemp = newString[temp[i].length]; for(intj = 0; j < temp[i].length; j++){ temptemp[j] =String.valueOf(temp[i][j].charAt(0)); } temp[i] = temptemp; } } } catch(BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } } String[] pingyinArray = Exchange(temp); Set<String> pinyinSet = newHashSet<String>(); for(inti = 0; i < pingyinArray.length; i++) { pinyinSet.add(pingyinArray[i]); } returnpinyinSet; } returnnull; } /** * 递归 * * @param strJaggedArray * @return */ publicstaticString[] Exchange(String[][] strJaggedArray) { String[][] temp = DoExchange(strJaggedArray); returntemp[0]; } /** * 递归 * * @param strJaggedArray * @return */ privatestaticString[][] DoExchange(String[][] strJaggedArray) { intlen = strJaggedArray.length; if(len >= 2) { intlen1 = strJaggedArray[0].length; intlen2 = strJaggedArray[1].length; intnewlen = len1 * len2; String[] temp = newString[newlen]; intIndex = 0; for(inti = 0; i < len1; i++) { for(intj = 0; j < len2; j++) { temp[Index] = strJaggedArray[0][i] + strJaggedArray[1][j]; Index++; } } String[][] newArray = newString[len - 1][]; for(inti = 2; i < len; i++) { newArray[i - 1] = strJaggedArray[i]; } newArray[0] = temp; returnDoExchange(newArray); } else{ returnstrJaggedArray; } } publicstaticvoidmain(String[] args) { String str = "单单芳abc"; System.out.println(makeStringByStringSet(str2Pinyin(str))); System.out.println(makeStringByStringSet(str2Pinyin(str, RET_PINYIN_TYPE_HEADCHAR))); } } |