码迷,mamicode.com
首页 > 编程语言 > 详细

java将汉字转成拼音

时间:2014-06-26 12:10:42      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:class   java   com   string   os   set   

package com.jframe.kit;

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;

/**
 * 汉字转换位汉语拼音,英文字符不变
 * @author xuke
 *
 */
public class Cn2SpellKit {
 
    /**
    * 汉字转换位汉语拼音首字母,英文字符不变
    * @param chines 汉字
    * @return 拼音
    */
    public static String converterToFirstSpell(String chines){     
        String pinyinName = "";
        char[] nameChar = chines.toCharArray();
        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.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];
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            }else{
             pinyinName += nameChar[i];
            }
        }
        return pinyinName;
    }
   
    public static String spell(String name){
     String pinyinName = "";
     if(name!=null && !"".equals(name)){
      if(name.length()<=2){
       pinyinName = converterToSpell(name);
      }else{
          pinyinName = converterToSpell(name.substring(0,1))+converterToFirstSpell(name.substring(1,name.length()));
         }
     }
     return pinyinName;
    }
   
    public static void main(String[] args) {
     
     System.out.println(spell("杨成"));
  System.out.println(spell("王帅(小)").toUpperCase());
 }
}

需要pinyin4j-2.5.0.jar

java将汉字转成拼音,布布扣,bubuko.com

java将汉字转成拼音

标签:class   java   com   string   os   set   

原文地址:http://blog.csdn.net/icss_s2/article/details/34795287

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!