免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1553 | 回复: 0
打印 上一主题 下一主题

Java汉字转拼音 [复制链接]

论坛徽章:
1
摩羯座
日期:2014-05-15 11:24:07
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-06-16 16:30 |只看该作者 |倒序浏览
  1. package com.joyce.pinyin4j;  
  2. import net.sourceforge.pinyin4j.PinyinHelper;  
  3. import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;  
  4. import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;  
  5. import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;  
  6. import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;  
  7. /**
  8. * PinYin4j
  9. * @author Joyce.Luo
  10. * @date 2015-5-14 下午01:04:38
  11. * @version V3.0
  12. * @since Tomcat6.0,Jdk1.6
  13. * @copyright Copyright (c) 2015
  14. */  
  15. public class PinyinUtil {  
  16.     public static void main(String[] args) {  
  17.         String str1 = PinyinUtil.toPinyinString('张');  
  18.         System.out.println("chinese char --> " + str1);  
  19.         String str2 = PinyinUtil.toPinyinString('c');  
  20.         System.out.println("english char --> " + str2);  
  21.         String str3 = PinyinUtil.toPinyinString("张三");  
  22.         System.out.println("chinese string --> " + str3);  
  23.         String str4 = PinyinUtil.toPinyinString("Hello World");  
  24.         System.out.println("english string --> " + str4);  
  25.         String str5 = PinyinUtil.toPinyinString("Hi 张三,hello world!");  
  26.         System.out.println("chinese and english --> " + str5);  
  27.     }  
  28.       
  29.     /**
  30.      * 获取Pinyin输出格式
  31.      * @return Pinyin输出格式
  32.      * @author Joyce.Luo
  33.      * @date 2015-5-14 下午01:40:10
  34.      * @version V3.0
  35.      * @since Tomcat6.0,Jdk1.6
  36.      * @copyright Copyright (c) 2015
  37.      */  
  38.     private static HanyuPinyinOutputFormat getPinyinFormat(){  
  39.         HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();  
  40.         /*
  41.          * UPPERCASE:大写 (ZHONG)
  42.          * LOWERCASE:小写 (zhong)
  43.          */  
  44.         format.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
  45.         /*
  46.          * WITHOUT_TONE:无音标 (zhong)
  47.          * WITH_TONE_NUMBER:1-4数字表示英标 (zhong4)
  48.          * WITH_TONE_MARK:直接用音标符(必须WITH_U_UNICODE否则异常) (zhòng)
  49.          */  
  50.         format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
  51.         /*
  52.          * WITH_V:用v表示ü (nv)
  53.          * WITH_U_AND_COLON:用"u:"表示ü (nu:)
  54.          * WITH_U_UNICODE:直接用ü (nü)
  55.          */  
  56.         format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);  
  57.          
  58.         // 返回输出格式  
  59.         return format;  
  60.     }  
  61.       
  62.     /**
  63.      * 将字符转换为Pinyin
  64.      * 若为英文字符,则直接输出
  65.      * 若字符为多音字,则取第一个
  66.      * @param c 待转换字符
  67.      * @return 转换后字符串
  68.      * @author Joyce.Luo
  69.      * @date 2015-5-14 下午01:34:55
  70.      * @version V3.0
  71.      * @since Tomcat6.0,Jdk1.6
  72.      * @copyright Copyright (c) 2015
  73.      */  
  74.     public static String toPinyinString(char c) {  
  75.         HanyuPinyinOutputFormat format = PinyinUtil.getPinyinFormat();  
  76.         try {  
  77.             String[] pinyin = PinyinHelper.toHanyuPinyinStringArray(c, format);  
  78.             if (null == pinyin || pinyin.length < 1) {  
  79.                 return String.valueOf(c);  
  80.             }  
  81.             return pinyin[0];  
  82.         } catch (Exception e) {  
  83.             e.printStackTrace();  
  84.             return null;  
  85.         }  
  86.     }  
  87.       
  88.     /**
  89.      * 字符串转换为Pinyin
  90.      * @param str 待转换字符串
  91.      * @return 转换后字符串
  92.      * @author Joyce.Luo
  93.      * @date 2015-5-14 下午01:38:17
  94.      * @version V3.0
  95.      * @since Tomcat6.0,Jdk1.6
  96.      * @copyright Copyright (c) 2015
  97.      */  
  98.     public static String toPinyinString(String str){  
  99.         if (null == str || "".equals(str)) {  
  100.             return null;  
  101.         }  
  102.         StringBuilder sb = new StringBuilder();  
  103.         String tempPinyin = null;  
  104.         for (int i = 0; i < str.length(); i++) {  
  105.             tempPinyin = PinyinUtil.toPinyinString(str.charAt(i));  
  106.             sb.append(tempPinyin);  
  107.         }  
  108.         return sb.toString();  
  109.     }  
  110. }  
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP