qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Java實現將漢字轉為拼音

          有時候為了方便操作程序的開發,需要將漢字轉為拼音等操作。下面這個是自己結合網上的資料,加上自己在公司項目中的親自實踐。完整的實現了將漢字轉為拼音的操作。這個Demo只是負責將其轉換,在main方法中測試,在實際需要中,只需要調用這個類中的方法即可。
            首先貼出測試結果:
            測試參數:
            漢字轉換為拼音
            漢字轉換為拼音
            main測試方法的代碼:
            1 public static void main(String[] args) {
            2         System.out.println(ToFirstChar("漢字轉換為拼音").toUpperCase()); //轉為首字母大寫
            3         System.out.println(ToPinyin("漢字轉換為拼音"));
            4     }
            本功能的實現時利用java開源庫,開發此程序需要一個jar包。本人用的是pinyin4j-2.5.0.jar。網上可以直接下載,也可以在其官網進行下載。在此不祥述。如果實在不樂意,可以點擊下載將進行這個jar包的下載。
          貼出實現該Demo的源碼:
          1 package com.red.test;
          2
          3 import net.sourceforge.pinyin4j.PinyinHelper;
          4 import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
          5 import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
          6 import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
          7 import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
          8
          9 /**
          10  * 漢字轉換為拼音
          11  * @author Red
          12  */
          13 public class PinyinDemo {
          14     /**
          15      * 測試main方法
          16      * @param args
          17      */
          18     public static void main(String[] args) {
          19         System.out.println(ToFirstChar("漢字轉換為拼音").toUpperCase()); //轉為首字母大寫
          20         System.out.println(ToPinyin("漢字轉換為拼音"));
          21     }
          22     /**
          23      * 獲取字符串拼音的第一個字母
          24      * @param chinese
          25      * @return
          26      */
          27     public static String ToFirstChar(String chinese){
          28         String pinyinStr = "";
          29         char[] newChar = chinese.toCharArray();  //轉為單個字符
          30         HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
          31         defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
          32         defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
          33         for (int i = 0; i < newChar.length; i++) {
          34             if (newChar[i] > 128) {
          35                 try {
          36                     pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0);
          37                 } catch (BadHanyuPinyinOutputFormatCombination e) {
          38                     e.printStackTrace();
          39                 }
          40             }else{
          41                 pinyinStr += newChar[i];
          42             }
          43         }
          44         return pinyinStr;
          45     }
          46
          47     /**
          48      * 漢字轉為拼音
          49      * @param chinese
          50      * @return
          51      */
          52     public static String ToPinyin(String chinese){
          53         String pinyinStr = "";
          54         char[] newChar = chinese.toCharArray();
          55         HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
          56         defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
          57         defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
          58         for (int i = 0; i < newChar.length; i++) {
          59             if (newChar[i] > 128) {
          60                 try {
          61                     pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];
          62                 } catch (BadHanyuPinyinOutputFormatCombination e) {
          63                     e.printStackTrace();
          64                 }
          65             }else{
          66                 pinyinStr += newChar[i];
          67             }
          68         }
          69         return pinyinStr;
          70     }
          71 }

          posted on 2014-11-03 09:22 順其自然EVO 閱讀(201) 評論(0)  編輯  收藏 所屬分類: 測試學習專欄

          <2014年11月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 灵川县| 宣武区| 濮阳县| 六枝特区| 喀喇| 宜黄县| 尚义县| 浑源县| 武夷山市| 井冈山市| 永顺县| 瑞丽市| 梧州市| 廉江市| 黄梅县| 汉源县| 巴塘县| 铜陵市| 固阳县| 三河市| 云南省| 诸城市| 伊吾县| 峨眉山市| 莱芜市| 新昌县| 涿州市| 夏邑县| 苏州市| 蒙山县| 游戏| 荣昌县| 灵台县| 绩溪县| 溆浦县| 荥经县| 潞城市| 固安县| 故城县| 成安县| 秦皇岛市|