[转载]获取汉语拼音

原文地址: http://www.iteye.com/topic/814845


Java代码   收藏代码
  1. package com.supben.util;  
  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. import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;  
  8. import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;  
  9.   
  10. public class SpellUtils {  
  11.   
  12.     /** 
  13.      * 获取全部拼音 
  14.      *  
  15.      * @param src 
  16.      *            原字符串 
  17.      * @return 
  18.      */  
  19.     public static String getFull(String src) {  
  20.   
  21.         char[] srcChar = src.toCharArray();  
  22.         String[] srcArry = new String[srcChar.length];  
  23.         HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();  
  24.   
  25.         // 设置格式  
  26.         format.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
  27.         format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
  28.         format.setVCharType(HanyuPinyinVCharType.WITH_V);  
  29.         String result = "";  
  30.         try {  
  31.             for (int i = 0; i < srcChar.length; i++) {  
  32.                 // 判断是否为汉字字符  
  33.                 if (Character.toString(srcChar[i])  
  34.                         .matches("[\\u4E00-\\u9FA5]+")) {  
  35.                     srcArry = PinyinHelper.toHanyuPinyinStringArray(srcChar[i],  
  36.                             format);  
  37.                     result += srcArry[0];  
  38.                 } else  
  39.                     result += Character.toString(srcChar[i]);  
  40.             }  
  41.             return result;  
  42.         } catch (BadHanyuPinyinOutputFormatCombination e1) {  
  43.             e1.printStackTrace();  
  44.         }  
  45.         return result;  
  46.     }  
  47.   
  48.     /** 
  49.      * 获取首字母拼音 
  50.      *  
  51.      * @param str 
  52.      *            原字符串 
  53.      * @return 
  54.      */  
  55.     public static String getFirst(String str) {  
  56.   
  57.         String result = "";  
  58.         for (int j = 0; j < str.length(); j++) {  
  59.             char word = str.charAt(j);  
  60.             String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);  
  61.             if (pinyinArray != null) {  
  62.                 result += pinyinArray[0].charAt(0);  
  63.             } else {  
  64.                 result += word;  
  65.             }  
  66.         }  
  67.         return result;  
  68.     }  
  69.       
  70.       
  71.   
  72. }  
资源包: http://download.csdn.net/download/lwydg/6842595

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值