| package com.doumee.core.utils; | 
|   | 
|   | 
| 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.exception.BadHanyuPinyinOutputFormatCombination; | 
| import org.apache.commons.lang3.StringUtils; | 
|   | 
| /** | 
|  * 汉语拼音帮助类 | 
|  *  | 
|  * @author yanglong | 
|  *  | 
|  */ | 
| public class PinYinUtil { | 
|   | 
|     /** | 
|      * 比较两个汉字的首字母 <br> | 
|      *  | 
|      * @param ch1 | 
|      * @param ch2 | 
|      * @return | 
|      */ | 
|     public static int comparatorWord(String ch1, String ch2) { | 
|         return getFirstSpell(ch2).compareTo(getFirstSpell(ch1)); | 
|     } | 
|   | 
|     public static void main(String[] args) { | 
|         String[] t = new String[]{ | 
|                 "QC/QD06林内水伺服16", | 
|                 "QS04林内白色13", | 
|         "QC05林内13", | 
|         "QS04林内白色16", | 
|                 "QC06(C08)林内水伺服13", | 
|                 "GD32林内16", | 
|                 "QC05林内16", | 
|                 "GD61R林内零冷水16", | 
|                 "QS41/QD31林内16", | 
|                 "QS41/QD31林内13", | 
|                 "E32(R32F)林内白色零冷水16", | 
|                 "E32(R32F)林内白色零冷水20", | 
|                 "E32(R32F)林内白色零冷水24", | 
|                 "GD32林内13", | 
|                 "QD32林内13", | 
|                 "QD32林内16", | 
|                 "QD33W林内16", | 
|                 "QD33W林内13", | 
|                 "E66林内水伺服13", | 
|                 "E86林内零冷水水伺服16", | 
|                 "E86林内零冷水水伺服20", | 
|                 "E86林内零冷水水伺服24", | 
|                 "X氧林内白零冷水水伺服16", | 
|                 "QS01林内白11", | 
|                 "GD31林内白13", | 
|                 "GD31林内白16", | 
|                 "GD32林内13", | 
|                 "GD32林内16", | 
|                 "QC05+SG林内零冷水", | 
|                 "D03林内经典13", | 
|                 "D03林内经典16", | 
|                 "D66W林内", | 
|                 "M16(M73)林内白水伺:服", | 
|                 "C01(QD01)林内白" | 
|   | 
|         }; | 
|         System.err.println(t.length); | 
|         for(String s : t){ | 
|             System.err.println("==============="+s); | 
|             System.err.println(getFullSpell(s)); | 
|         } | 
|   | 
|   | 
|     } | 
|     /** | 
|      * 获取汉字串拼音首字母,英文字符不变 | 
|      *  | 
|      * @param chinese | 
|      *            汉字串 | 
|      * @return 汉语拼音首字母 | 
|      */ | 
|     public static String getFirstSpell(String chinese) { | 
|         try { | 
|             StringBuffer pybf = new StringBuffer(); | 
|             char[] arr = chinese.toCharArray(); | 
|             HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); | 
|             defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); | 
|             defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); | 
|             for (int i = 0; i < arr.length; i++) { | 
|                 if (arr[i] > 128) { | 
|                     try { | 
|                         String[] temp = PinyinHelper.toHanyuPinyinStringArray( | 
|                                 arr[i], defaultFormat); | 
|                         if (temp != null) { | 
|                             pybf.append(temp[0].charAt(0)); | 
|                         } | 
|                     } catch (BadHanyuPinyinOutputFormatCombination e) { | 
|                         e.printStackTrace(); | 
|                     }catch (Exception e){ | 
|                         //e.printStackTrace(); | 
|                     } | 
|                 } else { | 
|                     pybf.append(arr[i]); | 
|                 } | 
|             } | 
|             return pybf.toString().replaceAll("\\W", "").trim(); | 
|   | 
|         }catch (Exception e){ | 
|   | 
|         } | 
|         return  null; | 
|     } | 
|     /** | 
|      * 获取汉字串拼音首字母,英文字符不变 | 
|      *  | 
|      * @param chinese | 
|      *            汉字串 | 
|      * @return 汉语拼音首字母 | 
|      */ | 
|     public static String getFirstFirstSpell(String chinese) { | 
|         if(StringUtils.isBlank(chinese)){ | 
|             return null; | 
|         } | 
|         StringBuffer pybf = new StringBuffer(); | 
|         chinese = chinese.substring(0,1); | 
|         char[] arr = chinese.toCharArray(); | 
|         HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); | 
|         defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); | 
|         defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); | 
|         for (int i = 0; i < arr.length; i++) { | 
|             if (arr[i] > 128) { | 
|                 try { | 
|                     String[] temp = PinyinHelper.toHanyuPinyinStringArray( | 
|                             arr[i], defaultFormat); | 
|                     if (temp != null) { | 
|                         pybf.append(temp[0].charAt(0)); | 
|                     } | 
|                 } catch (BadHanyuPinyinOutputFormatCombination e) { | 
|                     e.printStackTrace(); | 
|                 }catch (Exception e){ | 
|                     //e.printStackTrace(); | 
|                 } | 
|             } else { | 
|                 pybf.append(arr[i]); | 
|             } | 
|         } | 
|         return pybf.toString().replaceAll("\\W", "").trim().toUpperCase(); | 
|     } | 
|   | 
|     /** | 
|      * 获取汉字串拼音,英文字符不变 | 
|      *  | 
|      * @param chinese | 
|      *            汉字串 | 
|      * @return 汉语拼音 | 
|      */ | 
|     public static String getFullSpell(String chinese) { | 
|         try { | 
|             StringBuffer pybf = new StringBuffer(); | 
|             char[] arr = chinese.toCharArray(); | 
|             HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); | 
|             defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); | 
|             defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); | 
|             for (int i = 0; i < arr.length; i++) { | 
|                 if (arr[i] > 128) { | 
|                     try { | 
|                         pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], | 
|                                 defaultFormat)[0]); | 
|                     } catch (BadHanyuPinyinOutputFormatCombination e) { | 
|                         //e.printStackTrace(); | 
|                     }catch (Exception e){ | 
|                         //e.printStackTrace(); | 
|                     } | 
|                 } else { | 
|                     pybf.append(arr[i]); | 
|                 } | 
|             } | 
|             return pybf.toString(); | 
|         }catch (Exception e){ | 
|   | 
|         } | 
|         return  null; | 
|     } | 
| } |