nicole1108 发表于 2015-06-16 16:30

Java汉字转拼音

package com.joyce.pinyin4j;
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.HanyuPinyinVCharType;
/**
* PinYin4j
* @author Joyce.Luo
* @date 2015-5-14 下午01:04:38
* @version V3.0
* @since Tomcat6.0,Jdk1.6
* @copyright Copyright (c) 2015
*/
public class PinyinUtil {
    public static void main(String[] args) {
      String str1 = PinyinUtil.toPinyinString('张');
      System.out.println("chinese char --> " + str1);
      String str2 = PinyinUtil.toPinyinString('c');
      System.out.println("english char --> " + str2);
      String str3 = PinyinUtil.toPinyinString("张三");
      System.out.println("chinese string --> " + str3);
      String str4 = PinyinUtil.toPinyinString("Hello World");
      System.out.println("english string --> " + str4);
      String str5 = PinyinUtil.toPinyinString("Hi 张三,hello world!");
      System.out.println("chinese and english --> " + str5);
    }
      
    /**
   * 获取Pinyin输出格式
   * @return Pinyin输出格式
   * @author Joyce.Luo
   * @date 2015-5-14 下午01:40:10
   * @version V3.0
   * @since Tomcat6.0,Jdk1.6
   * @copyright Copyright (c) 2015
   */
    private static HanyuPinyinOutputFormat getPinyinFormat(){
      HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
      /*
         * UPPERCASE:大写 (ZHONG)
         * LOWERCASE:小写 (zhong)
         */
      format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
      /*
         * WITHOUT_TONE:无音标 (zhong)
         * WITH_TONE_NUMBER:1-4数字表示英标 (zhong4)
         * WITH_TONE_MARK:直接用音标符(必须WITH_U_UNICODE否则异常) (zhòng)
         */
      format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
      /*
         * WITH_V:用v表示ü (nv)
         * WITH_U_AND_COLON:用"u:"表示ü (nu:)
         * WITH_U_UNICODE:直接用ü (nü)
         */
      format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
         
      // 返回输出格式
      return format;
    }
      
    /**
   * 将字符转换为Pinyin
   * 若为英文字符,则直接输出
   * 若字符为多音字,则取第一个
   * @param c 待转换字符
   * @return 转换后字符串
   * @author Joyce.Luo
   * @date 2015-5-14 下午01:34:55
   * @version V3.0
   * @since Tomcat6.0,Jdk1.6
   * @copyright Copyright (c) 2015
   */
    public static String toPinyinString(char c) {
      HanyuPinyinOutputFormat format = PinyinUtil.getPinyinFormat();
      try {
            String[] pinyin = PinyinHelper.toHanyuPinyinStringArray(c, format);
            if (null == pinyin || pinyin.length < 1) {
                return String.valueOf(c);
            }
            return pinyin;
      } catch (Exception e) {
            e.printStackTrace();
            return null;
      }
    }
      
    /**
   * 字符串转换为Pinyin
   * @param str 待转换字符串
   * @return 转换后字符串
   * @author Joyce.Luo
   * @date 2015-5-14 下午01:38:17
   * @version V3.0
   * @since Tomcat6.0,Jdk1.6
   * @copyright Copyright (c) 2015
   */
    public static String toPinyinString(String str){
      if (null == str || "".equals(str)) {
            return null;
      }
      StringBuilder sb = new StringBuilder();
      String tempPinyin = null;
      for (int i = 0; i < str.length(); i++) {
            tempPinyin = PinyinUtil.toPinyinString(str.charAt(i));
            sb.append(tempPinyin);
      }
      return sb.toString();
    }
}
页: [1]
查看完整版本: Java汉字转拼音