有时,我们需要将中文汉字转为拼音存储,例如用于检索时,直接输入拼音或者首字母也能检索出我们想要的结果,这时我们便需要将汉字转拼音
- 1、首先我们在 pom.xml 文件中引入需要的 maven 依赖
<!-- 汉字转拼音 --> <dependency> <groupId>com.belerweb</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.1</version> </dependency>
- 2、汉字转拼音工具类代码如下 Pinyin4jUtil.java:
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; /** * 中文转化为拼音工具类 * * @author lizhou * */ public class Pinyin4jUtil { /** * 获得汉语拼音首字母 大写 * * @param chines 汉字 * @return */ public static String getAlpha2UpperCase(String chines) { String pinyinName = ""; char[] nameChar = chines.toCharArray(); HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE); defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); for (int i = 0; i < nameChar.length; i++) { if (nameChar[i] > 128) { try { pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0].charAt(0); } catch (BadHanyuPinyinOutputFormatCombination e) { System.out.println("获得汉语拼音首字母异常:}" + e.getMessage()); } } else { pinyinName += nameChar[i]; } } return pinyinName; } /** * 将字符串中的中文转化为拼音,英文字符不变 * * @param inputString 汉字 * @return */ public static String getPingYin(String inputString) { HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); format.setCaseType(HanyuPinyinCaseType.LOWERCASE); format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); format.setVCharType(HanyuPinyinVCharType.WITH_V); String output = ""; if (null != inputString && 0 < inputString.length() && !"null".equals(inputString)) { char[] input = inputString.trim().toCharArray(); try { for (int i = 0; i < input.length; i++) { if (Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")) { String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format); output += temp[0]; } else { output += Character.toString(input[i]); } } } catch (BadHanyuPinyinOutputFormatCombination e) { System.out.println("将字符串中的中文转化为拼音,英文字符不变异常 :" + e.getMessage()); } } else { return ""; } return output; } /** * 汉字转换位汉语拼音首字母,英文字符不变 * * @param chines 汉字 * @return 拼音 */ public static String converterToFirstSpell(String chines) { String pinyinName = ""; char[] nameChar = chines.toCharArray(); HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE); defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); for (int i = 0; i < nameChar.length; i++) { if (nameChar[i] > 128) { try { pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0].charAt(0); } catch (BadHanyuPinyinOutputFormatCombination e) { System.out.println("汉字转换位汉语拼音首字母,英文字符不变异常 :" + e.getMessage()); } } else { pinyinName += nameChar[i]; } } return pinyinName; } /** * 汉字转换位汉语拼音首字母,英文字符不变(小写) * * @param chines 汉字 * @return 拼音 */ public static String converterToFirstSpellSmal(String chines) { String pinyinName = ""; char[] nameChar = chines.toCharArray(); HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); for (int i = 0; i < nameChar.length; i++) { if (nameChar[i] > 128) { try { if (Character.toString(nameChar[i]).matches("[\\u4E00-\\u9FA5]+")) { pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0].charAt(0); } else { pinyinName += nameChar[i]; } } catch (BadHanyuPinyinOutputFormatCombination e) { System.out.println("汉字转换位汉语拼音首字母,英文字符不变异常:" + e.getMessage()); } } else { pinyinName += nameChar[i]; } } return pinyinName; } }