汉字转拼音(pinyin4j)
1、引入依赖
<dependency> <groupId>pinyin4j.sourceforge.net</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.0</version> </dependency>
2、代码
package net.biocloud.social.management.utils; 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.exception.BadHanyuPinyinOutputFormatCombination; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ChineseToEnglishHelper { private static final Logger LOG = LoggerFactory.getLogger(ChineseToEnglishHelper.class); /** * 姓名转换为拼音,如胡八一, BaYi Hu * * @param realName * @return */ public static String chineseToPinyin(String realName) { StringBuffer pybf = new StringBuffer(); char[] arr = realName.trim().toCharArray(); HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); String surname = ""; for (int i = 0; i < arr.length; i++) { if (arr[i] > 128) { try { //单个汉字转为拼音 String[] _t = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat); if (i == 0) { surname = _t[0].substring(0, 1).toUpperCase() + _t[0].substring(1, _t[0].length()); } else { pybf.append(_t[0].substring(0, 1).toUpperCase() + _t[0].substring(1, _t[0].length())); } if (i == arr.length - 1) { pybf.append(" " + surname); } } catch (BadHanyuPinyinOutputFormatCombination e) { LOG.info("chinese to english error, this is message" + e.getMessage()); throw new RuntimeException("chinese to english error"); } } else { pybf.append(arr[i]); } } return pybf.toString().trim(); } /** * 姓名转换为拼音,如胡八一, B Y Hu * * @param realName * @return */ public static String chineseToEnglish(String realName) { StringBuffer pybf = new StringBuffer(); char[] arr = realName.trim().toCharArray(); HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); String surname = ""; for (int i = 0; i < arr.length; i++) { if (arr[i] > 128) { try { //单个汉字转为拼音 String[] _t = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat); if (i == 0) { surname = _t[0].substring(0, 1).toUpperCase() + _t[0].substring(1, _t[0].length()); } else { pybf.append((arr.length == 2) ? _t[0].substring(0, 1).toUpperCase() : (i == 1) ? _t[0].substring(0, 1).toUpperCase() + " " : _t[0].substring(0, 1).toUpperCase()); } if (i == arr.length - 1) { pybf.append(" " + surname); } } catch (BadHanyuPinyinOutputFormatCombination e) { LOG.info("chinese to pinyin error, this is message" + e.getMessage()); throw new RuntimeException("chinese to pinyin error"); } } else { pybf.append(arr[i]); } } return pybf.toString().trim(); } public static void main(String arg[]) { System.out.println(chineseToPinyin("胡八一")); System.out.println(chineseToEnglish("胡八一")); } }