Java uses pinyin4j to convert Chinese characters to pinyin

   When encountering a requirement, you need to sort by the first letter of the user's name. This needs to obtain the pinyin corresponding to the Chinese characters, and suddenly I remembered the jar package pinyin4j, so I started to write a tool class for converting Chinese characters to pinyin. Record it here for future reference

1. Pom dependency

   
   
  1. <!-- https://mvnrepository.com/artifact/com.belerweb/pinyin4j -->
  2. <dependency>
  3. <groupId>com.belerweb</groupId>
  4. <artifactId>pinyin4j</artifactId>
  5. <version>2.5.0</version>
  6. </dependency>

2. Code

        This tool class implements the two methods of obtaining the pinyin of Chinese characters and obtaining the initials of Chinese characters
   
   
  1. package com.zxy.timecard.utils;
  2. import net.sourceforge.pinyin4j.PinyinHelper;
  3. /**
  4. * 拼音工具类
  5. * @author ZENG.XIAO.YAN
  6. * @date 2018年5月9日
  7. *
  8. */
  9. public class PinYinUtils {
  10. /**
  11. * 获取汉字首字母的方法。如: 张三 --> ZS
  12. * 说明:暂时解决不了多音字的问题,只能使用取多音字的第一个音的方案
  13. * @param hanzi 汉子字符串
  14. * @return 大写汉子首字母; 如果都转换失败,那么返回null
  15. */
  16. public static String getHanziInitials(String hanzi) {
  17. String result = null;
  18. if(null != hanzi && !"".equals(hanzi)) {
  19. char[] charArray = hanzi.toCharArray();
  20. StringBuffer sb = new StringBuffer();
  21. for (char ch : charArray) {
  22. // 逐个汉字进行转换, 每个汉字返回值为一个String数组(因为有多音字)
  23. String[] stringArray = PinyinHelper.toHanyuPinyinStringArray(ch);
  24. if(null != stringArray) {
  25. sb.append(stringArray[0].charAt(0));
  26. }
  27. }
  28. if(sb.length() > 0) {
  29. result = sb.toString().toUpperCase();
  30. }
  31. }
  32. return result;
  33. }
  34. /**
  35. * 获取汉字拼音的方法。如: 张三 --> zhangsan
  36. * 说明:暂时解决不了多音字的问题,只能使用取多音字的第一个音的方案
  37. * @param hanzi 汉子字符串
  38. * @return 汉字拼音; 如果都转换失败,那么返回null
  39. */
  40. public static String getHanziPinYin(String hanzi) {
  41. String result = null;
  42. if(null != hanzi && !"".equals(hanzi)) {
  43. char[] charArray = hanzi.toCharArray();
  44. StringBuffer sb = new StringBuffer();
  45. for (char ch : charArray) {
  46. // 逐个汉字进行转换, 每个汉字返回值为一个String数组(因为有多音字)
  47. String[] stringArray = PinyinHelper.toHanyuPinyinStringArray(ch);
  48. if(null != stringArray) {
  49. // 把第几声这个数字给去掉
  50. sb.append(stringArray[0].replaceAll("\\d", ""));
  51. }
  52. }
  53. if(sb.length() > 0) {
  54. result = sb.toString();
  55. }
  56. }
  57. return result;
  58. }
  59. public static void main(String[] args) {
  60. System.out.println(PinYinUtils.getHanziInitials("袁素芳"));
  61. System.out.println(PinYinUtils.getHanziPinYin("袁素芳"));
  62. }
  63. }

3. Summary

    The functions in the pinyin4j jar are all encapsulated, and they only need to be called directly; so the tool class is relatively simple to write.
    The existing problem: the polyphonic word is not handled well, only the first pronunciation of the word is taken

    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326147618&siteId=291194637