Java漢字轉拼音pinyin4j

來源:互聯網
上載者:User

標籤:java   pinyin   漢字轉拼音   pinyin4j   

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[0];} 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();}}


Java漢字轉拼音pinyin4j

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.