1 importnet.sourceforge.pinyin4j.PinyinHelper;2 importnet.sourceforge.pinyin4j.format.HanyuPinyinCaseType;3 importnet.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;4 importnet.sourceforge.pinyin4j.format.HanyuPinyinToneType;5 importnet.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;6
7 /**
8 * 拼音操作工具类9 *10 */
11 public classPinyinUtil {12 /**
13 * 将字符串转换成拼音数组14 *15 *@paramsrc16 *@return
17 */
18 public staticString[] stringToPinyin(String src) {19 return stringToPinyin(src, false, null);20 }21
22 /**
23 * 将字符串转换成拼音数组24 *25 *@paramsrc26 *@return
27 */
28 public staticString[] stringToPinyin(String src, String separator) {29 return stringToPinyin(src, true, separator);30 }31
32 /**
33 * 将字符串转换成拼音数组34 *35 *@paramsrc36 *@paramisPolyphone37 * 是否查出多音字的所有拼音38 *@paramseparator39 * 多音字拼音之间的分隔符40 *@return
41 */
42 public static String[] stringToPinyin(String src, booleanisPolyphone,43 String separator) {44 //判断字符串是否为空
45 if ("".equals(src) || null ==src) {46 return null;47 }48 char[] srcChar =src.toCharArray();49 int srcCount =srcChar.length;50 String[] srcStr = newString[srcCount];51
52 for (int i = 0; i < srcCount; i++) {53 srcStr[i] =charToPinyin(srcChar[i], isPolyphone, separator);54 }55 returnsrcStr;56 }57
58 /**
59 * 将单个字符转换成拼音60 *61 *@paramsrc62 *@return
63 */
64 public static String charToPinyin(char src, booleanisPolyphone, String separator) {65 //创建汉语拼音处理类
66 HanyuPinyinOutputFormat defaultFormat = newHanyuPinyinOutputFormat();67 //输出设置,大小写,音标方式
68 defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);69 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);70
71 StringBuffer tempPinying = newStringBuffer();72
73 //如果是中文
74 if (src > 128) {75 try{76 //转换得出结果
77 String[] strs =PinyinHelper.toHanyuPinyinStringArray(src, defaultFormat);78
79 //是否查出多音字,默认是查出多音字的第一个字符
80 if (isPolyphone && null !=separator) {81 for (int i = 0; i < strs.length; i++) {82 tempPinying.append(strs[i]);83 if (strs.length != (i + 1)) {84 //多音字之间用特殊符号间隔起来
85 tempPinying.append(separator);86 }87 }88 } else{89 tempPinying.append(strs[0]);90 }91
92 } catch(BadHanyuPinyinOutputFormatCombination e) {93 e.printStackTrace();94 }95 } else{96 tempPinying.append(src);97 }98
99 returntempPinying.toString();100
101 }102
103 public staticString hanziToPinyin(String hanzi) {104 return hanziToPinyin(hanzi, " ");105 }106
107 /**
108 * 将汉字转换成拼音109 *110 *@paramhanzi111 *@paramseparator112 *@return
113 */
114 @SuppressWarnings("deprecation")115 public staticString hanziToPinyin(String hanzi, String separator) {116 //创建汉语拼音处理类
117 HanyuPinyinOutputFormat defaultFormat = newHanyuPinyinOutputFormat();118 //输出设置,大小写,音标方式
119 defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);120 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);121
122 String pinyingStr = "";123 try{124 pinyingStr =PinyinHelper.toHanyuPinyinString(hanzi, defaultFormat, separator);125 } catch(BadHanyuPinyinOutputFormatCombination e) {126 e.printStackTrace();127 }128 returnpinyingStr;129 }130
131 /**
132 * 将字符串数组转换成字符串133 *134 *@paramstr135 *@paramseparator136 * 各个字符串之间的分隔符137 *@return
138 */
139 public staticString stringArrayToString(String[] str, String separator) {140 StringBuffer sb = newStringBuffer();141 for (int i = 0; i < str.length; i++) {142 sb.append(str[i]);143 if (str.length != (i + 1)) {144 sb.append(separator);145 }146 }147 returnsb.toString();148 }149
150 /**
151 * 简单的将各个字符数组之间连接起来152 *153 *@paramstr154 *@return
155 */
156 public staticString stringArrayToString(String[] str) {157 return stringArrayToString(str, "");158 }159
160 /**
161 * 将字符数组转换成字符串162 *163 *@paramstr164 *@paramseparator165 * 各个字符串之间的分隔符166 *@return
167 */
168 public static String charArrayToString(char[] ch, String separator) {169 StringBuffer sb = newStringBuffer();170 for (int i = 0; i < ch.length; i++) {171 sb.append(ch[i]);172 if (ch.length != (i + 1)) {173 sb.append(separator);174 }175 }176 returnsb.toString();177 }178
179 /**
180 * 将字符数组转换成字符串181 *182 *@paramstr183 *@return
184 */
185 public static String charArrayToString(char[] ch) {186 return charArrayToString(ch, " ");187 }188
189 /**
190 * 取汉字的首字母191 *192 *@paramsrc193 *@paramisCapital194 * 是否是大写195 *@return
196 */
197 public static char[] getHeadByChar(char src, booleanisCapital) {198 //如果不是汉字直接返回
199 if (src <= 128) {200 return new char[] { src };201 }202 //获取所有的拼音
203 String[] pinyingStr =PinyinHelper.toHanyuPinyinStringArray(src);204 //过滤中文符号
205 if (pinyingStr == null) {206 return new char[] { src };207 }208 //创建返回对象
209 int polyphoneSize =pinyingStr.length;210 char[] headChars = new char[polyphoneSize];211 int i = 0;212 //截取首字符
213 for(String s : pinyingStr) {214 char headChar = s.charAt(0);215 //首字母是否大写,默认是小写
216 if(isCapital) {217 headChars[i] =Character.toUpperCase(headChar);218 } else{219 headChars[i] =headChar;220 }221 i++;222 }223
224 returnheadChars;225 }226
227 /**
228 * 取汉字的首字母(默认是大写)229 *230 *@paramsrc231 *@return
232 */
233 public static char[] getHeadByChar(charsrc) {234 return getHeadByChar(src, true);235 }236
237 /**
238 * 查找字符串首字母239 *240 *@paramsrc241 *@return
242 */
243 public staticString[] getHeadByString(String src) {244 return getHeadByString(src, true);245 }246
247 /**
248 * 查找字符串首字母249 *250 *@paramsrc251 *@paramisCapital252 * 是否大写253 *@return
254 */
255 public static String[] getHeadByString(String src, booleanisCapital) {256 return getHeadByString(src, isCapital, null);257 }258
259 /**
260 * 查找字符串首字母261 *262 *@paramsrc263 *@paramisCapital264 * 是否大写265 *@paramseparator266 * 分隔符267 *@return
268 */
269 public static String[] getHeadByString(String src, booleanisCapital, String separator) {270 char[] chars =src.toCharArray();271 String[] headString = newString[chars.length];272 int i = 0;273 for (charch : chars) {274
275 char[] chs =getHeadByChar(ch, isCapital);276 StringBuffer sb = newStringBuffer();277 if (null !=separator) {278 int j = 1;279
280 for (charch1 : chs) {281 sb.append(ch1);282 if (j !=chs.length) {283 sb.append(separator);284 }285 j++;286 }287 } else{288 sb.append(chs[0]);289 }290 headString[i] =sb.toString();291 i++;292 }293 returnheadString;294 }295
296 public staticString getPinyin(String hanzi) {297 returnstringArrayToString(stringToPinyin(hanzi));298 }299 }