/** * 生成助记码 * 取首字母,如果字符不是中文取字符 * @param src * @return */public static String hanyuToPinYing(String src, boolean isFullHanyu) { if (StringUtils.isEmpty(src)) return null; StringBuilder pinyinBuf = new StringBuilder(); HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat(); outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); outputFormat.setVCharType(HanyuPinyinVCharType.WITH_V); try { for (int i = 0; i < src.length(); i++) { String[] pinYins = PinyinHelper.toHanyuPinyinStringArray(src.charAt(i), outputFormat); if (pinYins != null && pinYins.length > 0) {// 汉语 // 是否取全字母 if (isFullHanyu) { pinyinBuf.append(pinYins[0]); } else { pinyinBuf.append(pinYins[0].substring(0, 1)); } } else {// 非汉语 pinyinBuf.append(src.charAt(i)); } } } catch (BadHanyuPinyinOutputFormatCombination e) { e.PRintStackTrace(); } return pinyinBuf.toString();}
新闻热点
疑难解答