汉字转拼音工具类java

  1 package com.baihui.core.utils;
  2 import net.sourceforge.pinyin4j.PinyinHelper;
  3 import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
  4 import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
  5 import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
  6 import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
  7 import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
  8 /**
  9  * 
 10  * @author 
 11  * @Description 汉字到拼音的处理类
 12  */
 13 public class GetPinyinUtil {
 14 
 15     /**
 16      * 得到 全拼
 17      * 
 18      * @param src
 19      * @return
 20      */
 21     public static String getPingYin(String src) {
 22         char[] t1 = null;
 23         t1 = src.toCharArray();
 24         String[] t2 = new String[t1.length];
 25         HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
 26         t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
 27         t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
 28         t3.setVCharType(HanyuPinyinVCharType.WITH_V);
 29         String t4 = "";
 30         int t0 = t1.length;
 31         try {
 32             for (int i = 0; i < t0; i++) {
 33                 // 判断是否为汉字字符
 34                 if (java.lang.Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
 35                     t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
 36                     t4 += t2[0];
 37                 } else {
 38                     t4 += java.lang.Character.toString(t1[i]);
 39                 }
 40             }
 41             return t4;
 42         } catch (BadHanyuPinyinOutputFormatCombination e1) {
 43             e1.printStackTrace();
 44         }
 45         return t4;
 46     }
 47 
 48     /**
 49      * 得到中文首字母
 50      * 
 51      * @param str
 52      * @return
 53      */
 54     public static String getPinYinHeadChar(String str) {
 55         String convert = "";
 56         for (int j = 0; j < str.length(); j++) {
 57             char word = str.charAt(j);
 58             String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
 59             if (pinyinArray != null) {
 60                 convert += pinyinArray[0].charAt(0);
 61             } else {
 62                 convert += word;
 63             }
 64         }
 65         return convert;
 66     }
 67     
 68     /**
 69      * 验证是否包含中文
 70      */
 71     public static boolean containsChinese(String s) {
 72         if ((s == null) || ("".equals(s.trim())))
 73           return false;
 74         for (int i = 0; i < s.length(); ++i)
 75           if (isChinese(s.charAt(i)))
 76             return true;
 77 
 78         return false;
 79     }
 80 
 81     public static boolean isChinese(char a) {
 82         int v = a;
 83         return ((v >= 19968) && (v <= 171941));
 84     }
 85 
 86     /**
 87      * 小于6个字符的字符串得到中文首字母 
 88      */
 89     public static String getDocFieldValByPinyin(String str){
 90         return str;
 91         
 92         /*if(str.length()>6)
 93             return str;
 94         if(!containsChinese(str))
 95             return str;
 96         return str+" "+getPinYinHeadChar(str);*/
 97     }
 98     
 99 
100     /**
101      * 将字符串转移为ASCII码
102      * 
103      * @param cnStr
104      * @return
105      */
106     public static String getCnASCII(String cnStr) {
107         StringBuffer strBuf = new StringBuffer();
108         byte[] bGBK = cnStr.getBytes();
109         for (int i = 0; i < bGBK.length; i++) {
110             // System.out.println(Integer.toHexString(bGBK[i]&0xff));
111             strBuf.append(Integer.toHexString(bGBK[i] & 0xff));
112         }
113         return strBuf.toString();
114     }
115     public static String GetPyString(String chinese) throws BadHanyuPinyinOutputFormatCombination{
116         HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
117         String chineseTerm;
118         boolean isFirstChar = true;
119         if (isFirstChar) {
120           StringBuilder sb = new StringBuilder();
121           for (int i = 0; i < chinese.length(); ++i) {
122             String[] array = PinyinHelper.toHanyuPinyinStringArray(chinese.charAt(i), outputFormat);
123             if (array != null) { if (array.length == 0)
124                 continue;
125     
126               String s = array[0];
127               char c = s.charAt(0);
128     
129               sb.append(c); }
130           }
131           chineseTerm = sb.toString();
132         } else {
133           chineseTerm = PinyinHelper.toHanyuPinyinString(chinese, outputFormat, "");
134         }
135         return chineseTerm;
136    }
137 
138     public static void main(String[] args) {
139         System.out.println("----------"+getPingYin("行业"));
140     }
141 
142 }

猜你喜欢

转载自www.cnblogs.com/zhanghongjie/p/9239829.html