java實現將漢字轉為拼音


  有時候為了方便操作程序的開發,需要將漢字轉為拼音等操作。下面這個是自己結合網上的資料,加上自己在公司項目中的親自實踐。完整的實現了將漢字轉為拼音的操作。這個Demo只是負責將其轉換,在main方法中測試,在實際需要中,只需要調用這個類中的方法即可。本人也是在學習中總結,歡迎各位大神拍磚指教,本人郵箱:it_red@sina.com。轉載本博客時請在文章明顯位置標明文章出處(itRed的博客:http://www.cnblogs.com/itred)。

   首先貼出測試結果:

      

   測試參數:

        漢字轉換為拼音

        漢字轉換為拼音

     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 }

 

   作者:itRed
   出處:http://itred.cnblogs.com
   版權聲明:本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段說明,且在文章明顯位置給出原文鏈接,否則保留追究法律責任的權利。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM