汉字转拼音

Posted 叶枫声

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了汉字转拼音相关的知识,希望对你有一定的参考价值。

  1 package com.lenovo.mvs.utils;
  2 
  3 import static net.sourceforge.pinyin4j.PinyinHelper.toHanyuPinyinStringArray;
  4 
  5 import org.slf4j.Logger;
  6 import org.slf4j.LoggerFactory;
  7 
  8 import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
  9 import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
 10 import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
 11 import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
 12 import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
 13 
 14 /**
 15  * Chinese characters to pinyin
 16  * 
 17  * @author songyz
 18  *
 19  */
 20 public class ChineseUtil {
 21 
 22     private static final Logger logger = LoggerFactory.getLogger(ChineseUtil.class);
 23 
 24     /**
 25      * Get the complete pinyin
 26      * 
 27      * @param name
 28      * @return
 29      */
 30     public static String getCompletePingYin(String name) {
 31         char[] t1 = null;
 32         t1 = name.toCharArray();
 33         String[] t2 = new String[t1.length];
 34         HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
 35         t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
 36         t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
 37         t3.setVCharType(HanyuPinyinVCharType.WITH_V);
 38         String t4 = "";
 39         int t0 = t1.length;
 40         try {
 41             for (int i = 0; i < t0; i++) {
 42                 // 判断是否为汉字字符
 43                 if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
 44                     t2 = toHanyuPinyinStringArray(t1[i], t3);
 45                     t4 += t2[0];
 46                 } else
 47                     t4 += Character.toString(t1[i]);
 48             }
 49             // System.out.println(t4);
 50             return t4;
 51         } catch (BadHanyuPinyinOutputFormatCombination e) {
 52             logger.error("ChineseToEnglish for [{}] getFullPingYin is failure ! reason is [{}]", name, e.getMessage());
 53             throw new RuntimeException(e);
 54         }
 55     }
 56 
 57     /**
 58      * To obtain a simple pinyin
 59      * 
 60      * @param name
 61      * @return
 62      */
 63     public static String getSimplePingYin(String name) {
 64         if (name.length() > 1) {
 65             return getCompletePingYin(name.substring(0, 1)) + getPinYinHeadChar(name.substring(1));
 66         } else {
 67             return getCompletePingYin(name);
 68         }
 69 
 70     }
 71 
 72     /**
 73      * Get pinyin initials
 74      * 
 75      * @param name
 76      * @return
 77      */
 78     public static String getPinYinHeadChar(String name) {
 79         String convert = "";
 80         for (int j = 0; j < name.length(); j++) {
 81             char word = name.charAt(j);
 82             String[] pinyinArray = toHanyuPinyinStringArray(word);
 83             if (pinyinArray != null) {
 84                 convert += pinyinArray[0].charAt(0);
 85             } else {
 86                 convert += word;
 87             }
 88         }
 89         return convert;
 90     }
 91 
 92     // 将字符串转移为ASCII码
 93     public static String getCnASCII(String cnStr) {
 94         StringBuffer strBuf = new StringBuffer();
 95         byte[] bGBK = cnStr.getBytes();
 96         for (int i = 0; i < bGBK.length; i++) {
 97             strBuf.append(Integer.toHexString(bGBK[i] & 0xff));
 98         }
 99         return strBuf.toString();
100     }
101 
102     public static void main(String[] args) {
103         System.out.println(getPinYinHeadChar("宋印赠"));
104         System.out.println(getSimplePingYin("宋印赠"));
105         System.out.println(getCompletePingYin("宋印赠"));
106         System.out.println(getCnASCII("宋印赠"));
107     }
108 }

需要引入jar包支持,本人习惯maven地址,如下
1             <dependency>
2             <groupId>com.belerweb</groupId>
3             <artifactId>pinyin4j</artifactId>
4             <version>2.5.0</version>
5         </dependency>            

 





 

以上是关于汉字转拼音的主要内容,如果未能解决你的问题,请参考以下文章

C#汉字转拼音(支持多音字)

php汉字转拼音

C#汉字转拼音(支持多音字)

java怎么根据汉字获取字的拼音首字母

Python 汉字转拼音

汉字转拼音