Java汉字转拼音工具类

Java汉字转拼音的类。使用pinyin4j实现

 

[java]  view plain  copy
  1. package com.pinyin4j;  
  2.   
  3. import net.sourceforge.pinyin4j.PinyinHelper;  
  4. import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;  
  5. import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;  
  6. import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;  
  7.   
  8. /** 
  9.  * 汉字转拼音工具类<br> 
  10.  * 基于 pinyin4j 
  11.  *  
  12.  * @author Jayden 
  13.  *  
  14.  */  
  15. public class PinyinUtil {  
  16.   
  17.     private static PinyinUtil instance;  
  18.   
  19.     public static PinyinUtil getInstance() {  
  20.         if (instance == null) {  
  21.             instance = new PinyinUtil();  
  22.         }  
  23.         return instance;  
  24.     }  
  25.   
  26.     private HanyuPinyinOutputFormat outputFormat = null;  
  27.   
  28.     private HanyuPinyinOutputFormat getOutputFormat() {  
  29.         if (outputFormat == null) {  
  30.             outputFormat = new HanyuPinyinOutputFormat();  
  31.             outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
  32.             outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
  33.         }  
  34.         return outputFormat;  
  35.     }  
  36.   
  37.     /** 
  38.      * 获取字符串中所有字符首字母 
  39.      *  
  40.      * @param str 
  41.      * @return 
  42.      * @throws Exception 
  43.      */  
  44.     public String getStringInitial(String str) {  
  45.         StringBuffer sbf = new StringBuffer();  
  46.         if (str != null) {  
  47.             for (int i = 0; i < str.length(); i++) {  
  48.                 sbf.append(getCharInitial(str.charAt(i)));  
  49.             }  
  50.         }  
  51.         return sbf.toString();  
  52.     }  
  53.   
  54.     /** 
  55.      * 获取中文字符首字母 
  56.      *  
  57.      * @param c 
  58.      * @return 
  59.      * @throws Exception 
  60.      */  
  61.     public String getCharInitial(char c) {  
  62.         try {  
  63.             String[] strs = PinyinHelper.toHanyuPinyinStringArray(c,  
  64.                     getOutputFormat());  
  65.             String initial = "";  
  66.             if (strs != null && strs.length > 0) {  
  67.                 String str = strs[0];  
  68.                 if (str != null && str.length() > 0) {  
  69.                     initial += str.charAt(0);  
  70.                 }  
  71.             }  
  72.             return initial;  
  73.         } catch (Exception ex) {  
  74.             ex.printStackTrace();  
  75.             return c + "";  
  76.         }  
  77.     }  
  78.   
  79.     public String getStringPinyin(String chines) {  
  80.   
  81.         char[] nameChar = chines.toCharArray();  
  82.         String pinyinStr = "";  
  83.         for (int i = 0; i < nameChar.length; i++) {  
  84.             try {  
  85.                 char cha=nameChar[i];  
  86.                 if (nameChar[i] > 128) {  
  87.                     pinyinStr += PinyinHelper.toHanyuPinyinStringArray(cha, getOutputFormat())[0];  
  88.                 }  
  89.             } catch (Exception ex) {  
  90.                 ex.printStackTrace();  
  91.                 pinyinStr += nameChar[i];  
  92.             }  
  93.         }  
  94.         return pinyinStr;  
  95.     }  
  96.   
  97.     /** 
  98.      * 获取字符全拼 
  99.      *  
  100.      * @param c 
  101.      * @return 
  102.      */  
  103.     public String getCharPinyin(char c) {  
  104.   
  105.         try {  
  106.             String[] strs = PinyinHelper.toHanyuPinyinStringArray(c,  
  107.                     getOutputFormat());  
  108.             String str = "";  
  109.             if (strs != null && strs.length > 0) {  
  110.                 str = strs[0];  
  111.             }  
  112.             return str;  
  113.         } catch (Exception ex) {  
  114.             ex.printStackTrace();  
  115.             return c + "";  
  116.         }  
  117.     }  
  118.   
  119.     public static void main(String[] args) {  
  120.         String stringInitial = PinyinUtil.getInstance().getStringInitial("玥是个生僻字一般的拼音码解析不了");  
  121.         System.out.println(stringInitial);  
  122.   
  123.         String stringPinyin = PinyinUtil.getInstance().getStringPinyin("玥是个生僻字一般的拼音码解析不了");  
  124.         System.out.println(stringPinyin);  
  125.   
  126.     }  
  127. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值