• java 中汉字转拼音


    需要引入的jar

    1. <dependency>
    2. <groupId>com.belerwebgroupId>
    3. <artifactId>pinyin4jartifactId>
    4. <version>2.5.0version>
    5. dependency>

    实现工具类

    1. package com.julong.util;
    2. import java.util.regex.Pattern;
    3. import org.slf4j.Logger;
    4. import org.slf4j.LoggerFactory;
    5. import net.sourceforge.pinyin4j.PinyinHelper;
    6. import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
    7. import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
    8. import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
    9. import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
    10. import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
    11. /**
    12. * 中文转拼音工具类
    13. * @author julong
    14. * @date 2023年11月22日 上午11:20:30
    15. * @desc
    16. */
    17. public class PinyinUtils {
    18. //日志
    19. private static final Logger logger = LoggerFactory.getLogger(PinyinUtils.class);
    20. /**
    21. * ^[\u2E80-\u9FFF]+$ 匹配所有东亚区的语言
    22. * ^[\u4E00-\u9FFF]+$ 匹配简体和繁体
    23. * ^[\u4E00-\u9FA5]+$ 匹配简体
    24. */
    25. public final static String REGEX="^[\u4E00-\u9FFF]+$";
    26. //设置格式
    27. public static HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();;
    28. public PinyinUtils(){
    29. }
    30. /**
    31. * 汉子转拼音
    32. * @param data 汉子转拼音
    33. * @return han zi huan pin yin
    34. * @author julong
    35. * @date 2023年11月22日 上午11:21:50
    36. * @desc
    37. */
    38. public static String convertPinyin(String data){
    39. logger.debug("【汉子转拼音工具】-转换拼音输入参数-data:{}",data);
    40. if(StringUtils.isBlank(data)){
    41. return "";
    42. }
    43. outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    44. outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    45. outputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
    46. String newData = "";
    47. //转为 char
    48. char[] chars = data.toCharArray();
    49. try {
    50. //判断是否为汉子
    51. for (char c : chars) {
    52. if(Pattern.matches(REGEX,String.valueOf(c))){
    53. //转码
    54. String[] a = PinyinHelper.toHanyuPinyinStringArray(c,outputFormat);
    55. newData = newData + a[0] +" ";
    56. }else{
    57. newData = newData + c + " ";
    58. }
    59. }
    60. } catch (BadHanyuPinyinOutputFormatCombination e) {
    61. // TODO: handle exception
    62. logger.error("【汉子转拼音工具】-转换拼音出现问题:{}",e);
    63. return data;
    64. }
    65. return newData;
    66. }
    67. /**
    68. * 汉子转拼音
    69. * @param data 汉子转拼音
    70. * @param caseType HanyuPinyinCaseType
    71. * @return

      小写:han zi huan pin yin

      大写:HAN ZI HUAN PIN YIN

    72. * @author julong
    73. * @date 2023年11月22日 上午11:25:30
    74. * @desc
    75. */
    76. public static String convertPinyin(String data,HanyuPinyinCaseType caseType){
    77. logger.debug("【汉子转拼音工具】-转换拼音输入参数-data:{},caseType:{}",data,caseType.getName());
    78. if(StringUtils.isBlank(data)){
    79. return "";
    80. }
    81. outputFormat.setCaseType(caseType);
    82. outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    83. outputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
    84. String newData = "";
    85. //转为 char
    86. char[] chars = data.toCharArray();
    87. try {
    88. //判断是否为汉子
    89. for (char c : chars) {
    90. if(Pattern.matches(REGEX,String.valueOf(c))){
    91. //转码
    92. String[] a = PinyinHelper.toHanyuPinyinStringArray(c,outputFormat);
    93. newData = newData + a[0] +" ";
    94. }else{
    95. String str = String.valueOf(c);
    96. if(caseType.getName().equals(HanyuPinyinCaseType.UPPERCASE.getName())){
    97. str = str.toUpperCase();
    98. }
    99. newData = newData + str + " ";
    100. }
    101. }
    102. } catch (BadHanyuPinyinOutputFormatCombination e) {
    103. // TODO: handle exception
    104. logger.error("【汉子转拼音工具】-转换拼音出现问题:{}",e);
    105. return data;
    106. }
    107. return newData;
    108. }
    109. /**
    110. * 汉子转拼音首字母
    111. * @param data
    112. * @return
    113. * @author julong
    114. * @date 2023年11月22日 上午11:30:28
    115. * @desc
    116. */
    117. public static String convertPinyinInitials(String data){
    118. logger.debug("【汉子转拼音工具】-转换拼音输入参数-data:{}",data);
    119. if(StringUtils.isBlank(data)){
    120. return "";
    121. }
    122. String newData = "";
    123. //转为 char
    124. char[] chars = data.toCharArray();
    125. //判断是否为汉子
    126. for (char c : chars) {
    127. if(Pattern.matches(REGEX,String.valueOf(c))){
    128. //转码
    129. String[] a = PinyinHelper.toHanyuPinyinStringArray(c);
    130. newData = newData + a[0].charAt(0);
    131. }else{
    132. //不是汉子
    133. newData = newData + c;
    134. }
    135. }
    136. return newData;
    137. }
    138. public static void main(String[] args) {
    139. // TODO Auto-generated method stub
    140. String zhongwen = "中文转拼音,pin yin";
    141. System.out.println(convertPinyinInitials(zhongwen).toUpperCase());
    142. System.out.println(convertPinyin(zhongwen));
    143. System.out.println(convertPinyin(zhongwen,HanyuPinyinCaseType.LOWERCASE));
    144. System.out.println(convertPinyin(zhongwen,HanyuPinyinCaseType.UPPERCASE));
    145. }
    146. }

  • 相关阅读:
    解决Java获取数据库日期数据问题:格式为【YYYY/MM/DD】,获取的格式却转换为【YYYY-MM-DD hh:mm:ss】
    设计模式—创建型模式之建造者模式
    【Git LFS】huggingface 断点续传
    javaweb智慧洗衣店系统
    暑假加餐|有钱人和你想的不一样(第20天)+改进的多目标差分进化算法在电力系统环境经济调度中的应用(Python代码实现)
    Jackson ImmunoResearch 过敏测试
    万字整理 | 深入理解工作队列
    写给刚入学大数据专业或迷茫在为几两碎银转行的你
    网络安全笔记6——数字证书与公钥基础设施
    YoloV5/YoloV7优化:感受野注意力卷积运算(RFAConv),效果秒杀CBAM和CA等 | 即插即用系列
  • 原文地址:https://blog.csdn.net/u010416069/article/details/134550817