Pinyin4J使用方法

介绍

Pinyin4j是一个流行的Java库,支持中文字符和拼音之间的转换,拼音输出格式可以定制。
Pinyin4jUtils工具类是进行包装,进行支持一一些转换方法

支持方法

输入一个字符串可以给我转成首字母返回给我
例如:颐和园 
  1. 转换格式:全部大小YHY
  2. 转换格式:全部大写(中间加字符串*)Y*H*Y
  3. 转换格式:全部小写yhy
  4. 转换格式:全部小写(中间加字符串*)y*h*y
  5. 转换格式:返回首字母大写Y
  6. 转换格式:返回首字母小写y

使用方法

[java]  view plain  copy
  1. Pinyin4j pinyin4j = new Pinyin4j();  
  2. String first1 = pinyin4j.toPinYinUppercase("颐和园");  
  3. String first2 = pinyin4j.toPinYinUppercase("颐和园""**");  
  4. String first3 = pinyin4j.toPinYinLowercase("颐和园");  
  5. String first4 = pinyin4j.toPinYinLowercase("颐和园","**");  
  6. String first5 = pinyin4j.toPinYinUppercaseInitials("颐和园");  
  7. String first6 = pinyin4j.toPinYinLowercaseInitials("颐和园");  
  8. System.out.println(first1); //输出结果:YHY  
  9. System.out.println(first2); //输出结果:Y**H**Y  
  10. System.out.println(first3); //输出结果:yhy  
  11. System.out.println(first4); //输出结果:y**h**y  
  12. System.out.println(first5); //输出结果:Y  
  13. System.out.println(first6); //输出结果:y  

java工具类代码

[java]  view plain  copy
  1. import org.junit.Test;  
  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.exception.BadHanyuPinyinOutputFormatCombination;  
  7. /** 
  8.  * 汉字转换成拼音方法 
  9.  * @author 蔡龙 
  10.  */  
  11.   
  12. public class Pinyin4j {  
  13.       
  14. <span style="white-space:pre;"> </span>HanyuPinyinOutputFormat format = null;    
  15.     public static enum Type {    
  16.         UPPERCASE,            //全部大写    
  17.         LOWERCASE,            //全部小写    
  18.         FIRSTUPPER            //首字母大写  
  19.     }    
  20.     
  21.     public Pinyin4j(){    
  22.         format = new HanyuPinyinOutputFormat();    
  23.         format.setCaseType(HanyuPinyinCaseType.UPPERCASE);    
  24.         format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);    
  25.     }    
  26.     
  27.     /** 
  28.      * 转换全部大写  
  29.      * @param str 字符串 
  30.      * @return str为颐和园 ,return获取到的是YHY 
  31.      * @throws BadHanyuPinyinOutputFormatCombination 
  32.      */  
  33.     public String toPinYinUppercase(String str) throws BadHanyuPinyinOutputFormatCombination{    
  34.         return toPinYin(str, "", Type.UPPERCASE);    
  35.     }    
  36.     
  37.     /** 
  38.      * 转换全部大写 
  39.      * @param str 字符串 
  40.      * @param spera 转换字母间隔加的字符串,如果不需要为"" 
  41.      * @return str为颐和园 ,spera为** return获取到的是Y**H**Y 
  42.      * @throws BadHanyuPinyinOutputFormatCombination 
  43.      */  
  44.     public String toPinYinUppercase(String str,String spera) throws BadHanyuPinyinOutputFormatCombination{    
  45.         return toPinYin(str, spera, Type.UPPERCASE);    
  46.     }   
  47.       
  48.     /** 
  49.      * 转换全部小写 
  50.      * @param str 字符串 
  51.      * @return  str为颐和园 ,return获取到的是yhy 
  52.      * @throws BadHanyuPinyinOutputFormatCombination 
  53.      */  
  54.     public String toPinYinLowercase(String str) throws BadHanyuPinyinOutputFormatCombination{    
  55.         return toPinYin(str, "", Type.LOWERCASE);    
  56.     }    
  57.     
  58.     /** 
  59.      * 转换全部小写 
  60.      * @param str 字符串 
  61.      * @param spera 转换字母间隔加的字符串,如果不需要为"" 
  62.      * @return  str为颐和园 ,spera为** return获取到的是y**h**y 
  63.      * @throws BadHanyuPinyinOutputFormatCombination 
  64.      */  
  65.     public String toPinYinLowercase(String str,String spera) throws BadHanyuPinyinOutputFormatCombination{    
  66.         return toPinYin(str, spera, Type.LOWERCASE);    
  67.     }   
  68.       
  69.     /**  
  70.      * 获取拼音首字母(大写) 
  71.      * @param str 字符串 
  72.      * @return str为颐和园 ,return获取到的是Y 
  73.      * @throws BadHanyuPinyinOutputFormatCombination 异常信息 
  74.      */    
  75.     public String toPinYinUppercaseInitials(String str) throws BadHanyuPinyinOutputFormatCombination {    
  76.         String initials = null;  
  77.         String py = toPinYinUppercase(str);   
  78.         if(py.length()>1){  
  79.             initials = py.substring(01);  
  80.         }  
  81.         if(py.length()<=1){  
  82.             initials = py;  
  83.         }  
  84.         return initials.trim();    
  85.     }   
  86.       
  87.     /**  
  88.      * 获取拼音首字母(小写) 
  89.      * @param str 字符串 
  90.      * @return str为颐和园 ,return获取到的是y 
  91.      * @throws BadHanyuPinyinOutputFormatCombination 异常信息 
  92.      */    
  93.     public String toPinYinLowercaseInitials(String str) throws BadHanyuPinyinOutputFormatCombination {    
  94.         String initials = null;  
  95.         String py = toPinYinLowercase(str);   
  96.         if(py.length()>1){  
  97.             initials = py.substring(01);  
  98.         }  
  99.         if(py.length()<=1){  
  100.             initials = py;  
  101.         }  
  102.         return initials.trim();    
  103.     }   
  104.     
  105.     /**  
  106.      * 将str转换成拼音,如果不是汉字或者没有对应的拼音,则不作转换  
  107.      * @param str    字符串 
  108.      * @param spera  默认,可为"" 
  109.      * @param type   转换格式 
  110.      * @return 按照转换格式转换成字符串 
  111.      * @throws BadHanyuPinyinOutputFormatCombination 异常信息  
  112.      */    
  113.     public String toPinYin(String str, String spera, Type type) throws BadHanyuPinyinOutputFormatCombination {    
  114.         if(str == null || str.trim().length()==0) {   
  115.             return "";   
  116.         }  
  117.         if(type == Type.UPPERCASE) {   
  118.             format.setCaseType(HanyuPinyinCaseType.UPPERCASE);  
  119.         } else{    
  120.             format.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
  121.         }  
  122.         String py = "";    
  123.         String temp = "";    
  124.         String[] t;    
  125.         for(int i=0;i<str.length();i++){    
  126.             char c = str.charAt(i);    
  127.             if((int)c <= 128)  {  
  128.                 py += c;   
  129.             }else{    
  130.                 t = PinyinHelper.toHanyuPinyinStringArray(c, format);    
  131.                 if(t == null) {   
  132.                     py += c;    
  133.                 }else{    
  134.                     temp = t[0];    
  135.                     if(type == Type.FIRSTUPPER) {   
  136.                         temp = t[0].toUpperCase().charAt(0)+temp.substring(1);   
  137.                     }  
  138.                     if(temp.length()>=1){  
  139.                        <span style="white-space:pre;">  </span>temp = temp.substring(01);  
  140.                     }  
  141.                         py += temp+(i==str.length()-1?"":spera);    
  142.                     }    
  143.                 }    
  144.             }    
  145.             return py.trim();    
  146.         }    
  147. }  

猜你喜欢

转载自blog.csdn.net/qq_35232663/article/details/80445714