pinyin4j工具类

pinyin4j工具类Java工具类 2010-04-24 00:47:00 阅读69 评论0 字号:大中小
引入pinyin4j-2.5.0.jar包:
pinyin4j是一个功能强悍的汉语拼音工具包,主要是从汉语获取各种格式和需求的拼音,功能强悍,下面看看如何使用pinyin4j。

本人以前用AscII编码提取工具,效果不理想,现在用pinyin4j简单实现了一个。功能还不是很完美,陆续再改进吧。
package cn.itcast.crm.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class PingyinUtils {
    /**  
     * 汉字转换为汉语拼音首字母,英文字符不变  
     * @param chines 汉字  
     * @return 拼音
     */     
    public static String converterToFirstSpell(String chines){             
         String pinyinName = "";  
        
         //转化为字符
         char[] nameChar = chines.toCharArray();
//         for(int i=0;i
//             System.out.println(nameChar[i]);
//         }
        
         //汉语拼音格式输出类  
         HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        
         //输出设置,大小写,音标方式等  
         defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);      
         defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);      
        
         for (int i = 0; i < nameChar.length; i++) {      
             //如果是中文
             if (nameChar[i] > 128) {
                try {      
                     pinyinName +=
                           PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0].charAt(0);      
                 } catch (BadHanyuPinyinOutputFormatCombination e) {      
                     e.printStackTrace();      
                 }      
             }else{//为英文字符   
                 pinyinName += nameChar[i];      
             }      
         }      
        return pinyinName;      
     }      
       
    /**  
     * 汉字转换位汉语拼音,英文字符不变  
     * @param chines 汉字  
     * @return 拼音  
     */     
    public static String converterToSpell(String chines){              
        String pinyinName = "";      
        char[] nameChar = chines.toCharArray();      
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();      
        defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);      
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);      
        for (int i = 0; i < nameChar.length; i++) {      
            if (nameChar[i] > 128) {      
                try {      
                     pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0];      
                 } catch (BadHanyuPinyinOutputFormatCombination e) {      
                     e.printStackTrace();      
                 }      
             }else{      
                 pinyinName += nameChar[i];      
             }      
         }      
        return pinyinName;      
     }      
          
    public static void main(String[] args) {      
         System.out.println(converterToFirstSpell("欢迎光临我的博客"));  //欢迎来到最棒的Java中文社区
     }      
}
执行结果:hyglwdbk

你可能感兴趣的:(.net)