1. Problem description <br />Converting Chinese characters into corresponding pinyin or obtaining the first letter of the pinyin of Chinese characters is a common problem encountered in development. After obtaining the first letter of the pinyin or pinyin of the Chinese characters, we can greatly improve the user's experience in the recommendation or search department. For example, if the user enters "NH", we can associate words such as "Hello", "You will", "after the New Year", "connotation" and other words. In Java, the pinyin4j.jar tool is a good tool to convert Chinese characters into corresponding pinyin. Let’s introduce how to use this jar package.
2. Resource download
After downloading, unzip it and use pinyin4j-2.5.0.jar in the file directly.
3. Provide method
We can use the HanyuPinyinOutputFormat class to set the return method of pinyin, such as setting the upper and lower case of pinyin, phonetic symbol method, and display form of pinyin ü, as shown in the figure below:
Directly use the method in PinyinHelper to convert Chinese characters accordingly. There are three specific types, and how to test the three effects by yourself:
4. Writing code <br />For the functions we may use normally, I have made the following packages. The functions provided also have specific implementation steps and refer to the comments in the code:
package com.lulei.util; import java.util.ArrayList; import java.util.List; import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType; public class PinYinUtil { private static HanyuPinyinOutputFormat format = null; static { format = new HanyuPinyinOutputFormat(); //Pinyin lowercase format.setCaseType(HanyuPinyinCaseType.LOWERCASE); //No phonetic mode; WITH_TONE_NUMBER: 1-4 numbers represent British standard; WITH_TONE_MARK: Use phonetic symbols directly (WITH_U_UNICODE must be WITH_UNICODE otherwise exception format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); //V represents ü format.setVCharType(HanyuPinyinVCharType.WITH_V); } /** * @param str * @return * @Description: Return the pinyin of the string */ public static String[] getCharPinYinString(String str) { if (str == null || str.length() < 1) { return null; } List<String> result = new ArrayList<String>(); //Analyze the records in the string one by one for (int i = 0; i < str.length(); i++) { result = getCharPinYinString(str.charAt(i), result); } return result.toArray(new String[result.size()]); } /** * @param c * @param list * @return * @Description: Splice the pinyin of the character c into the record in the list*/ private static List<String> getCharPinYinString(char c, List<String> list) { String[] strs = getCharPinYinString(c); List<String> result = new ArrayList<String>(); //If the parsed pinyin is empty, determine whether character C is an English letter. If it is an English letter, add the value in the pinyin result if (strs == null) { if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { c = c <= 91 ? (char)(c + 32) : c; if (list == null || list.size() == 0) { result.add(c + ""); } else { for (String s : list) { result.add(s + c); } } return result; } return list; } //Combine the pinyin head of the character C and the existing pinyin head into a new record for (String str : strs) { if (list == null || list.size() == 0) { result.add(str); } else { for (String s : list) { result.add(s + str); } } } return result; } /** * @param c * @return * @Description: Return the pinyin of Chinese characters */ public static String[] getCharPinYinString(char c) { try { //Return the pinyin of character C return PinyinHelper.toHanyuPinyinStringArray(c, format); } catch (Exception e) { e.printStackTrace(); } return null; } /** * @param str * @return * @Description: Return the first letter of the pinyin of the string */ public static String[] getCharPinYinChar(String str) { if (str == null || str.length() < 1) { return null; } List<String> result = new ArrayList<String>(); //Analyze the records in the string one by one for (int i = 0; i < str.length(); i++) { result = getCharPinYinChar(str.charAt(i), result); } return result.toArray(new String[result.size()]); } /** * @param c * @param list * @return * @Description: Splice the first letter of the pinyin of the character c into the record in the list*/ private static List<String> getCharPinYinChar(char c, List<String> list) { char[] chars = getCharPinYinChar(c); List<String> result = new ArrayList<String>(); //If the parsed pinyin is empty, determine whether character C is an English letter. If it is an English letter, add value in the pinyin result if (chars == null) { if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { c = c < 91 ? (char)(c + 32) : c; if (list == null || list.size() == 0) { result.add(c + ""); } else { for (String s : list) { result.add(s + c); } } return result; } return list; } //Combine the pinyin initials of the character C and the existing pinyin initials into a new record for (char ch : chars) { if (list == null || list.size() == 0) { result.add(ch + ""); } else { for (String s : list) { result.add(s + ch); } } } return result; } /** * @param c * @return * @Description:Return the first letter of the Chinese character pinyin*/ public static char[] getCharPinYinChar(char c) { //The pinyin of the character C String[] strs = getCharPinYinString(c); if (strs != null) { //The first letter of the pinyin char[] chars = new char[strs.length]; for(int i = 0; i <chars.length; i++) { chars[i] = strs[i].charAt(0); } return chars; } return null; } public static void main(String[] args) { // TODO Auto-generated method stub char c = "Chongqing".charAt(0); String[] str = PinYinUtil.getCharPinYinString(c); for(String s : str) { System.out.println(s); } char[] chars = PinYinUtil.getCharPinYinChar(c); for(char c1 : chars) { System.out.println(c1); } str = PinYinUtil.getCharPinYinString("Chongqing c"); for(String s : str) { System.out.println(s); } str = PinYinUtil.getCharPinYinChar("Chongqing a"); for(String s : str) { System.out.println(s); } } } 5. Output result
The above is all the content of Java to convert Chinese characters to pinyin. I hope it will be helpful to everyone's learning.