獲取中文漢字字符串相應的拼音和首字母的大小寫
一、內容
使用 pinyin4j 技術,獲取一個字符串相應的拼音和首字符的大小寫。
需要 pinyin4j 的組建包,官網下載地址:http://pinyin4j.sourceforge.net/
官方文檔:http://pinyin4j.sourceforge.net/pinyin4j-doc/
二、源代碼
1 package cn.com.zfc.day014; 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 * @describe 中文轉化成拼音工具類 12 * @author zfc 13 * @date 2017年12月19日 上午11:32:59 14 */ 15 public class ChineseToPinYinUtil { 16 17 public static void main(String[] args) { 18 // 中文字符串首字母大寫 19 System.out.println("張富昌(中文字符串首字母大寫):" + toFirstCharUpCase("張富昌")); 20 // 中文字符串首字母小寫 21 System.out.println("張富昌(中文字符串首字母小寫):" + toFirstCharLowCase("張富昌")); 22 // 中文字符串拼音大寫 23 System.out.println("張富昌(中文字符串拼音大寫):" + toPinyinUpCase("張富昌")); 24 // 中文字符串拼音小寫 25 System.out.println("張富昌(中文字符串拼音小寫):" + toPinyinLowCase("張富昌")); 26 } 27 28 /** 29 * 獲取中文字符串相應的拼音首字母小寫 30 * 31 * @param chinese 32 * @return 33 * @author zfc 34 * 35 */ 36 public static String toFirstCharLowCase(String chinese) { 37 if (null == chinese) { 38 return null; 39 } 40 String pinyinStr = ""; 41 // 將字符串轉為字符數組 42 char[] newChar = chinese.toCharArray(); 43 HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); 44 // 設置小寫 45 defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); 46 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); 47 for (int i = 0; i < newChar.length; i++) { 48 if (newChar[i] > 128) { 49 try { 50 pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0); 51 } catch (BadHanyuPinyinOutputFormatCombination e) { 52 e.printStackTrace(); 53 } 54 } else { 55 pinyinStr += newChar[i]; 56 } 57 } 58 return pinyinStr; 59 } 60 61 /** 62 * 獲取中文字符串相應的拼音首字母大寫 63 * 64 * @param chinese 65 * @return 66 * @author zfc 67 * 68 */ 69 public static String toFirstCharUpCase(String chinese) { 70 if (null == chinese) { 71 return null; 72 } 73 String pinyinStr = ""; 74 // 將字符串轉為字符數組 75 char[] newChar = chinese.toCharArray(); 76 HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); 77 // 設置大寫 78 defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE); 79 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); 80 for (int i = 0; i < newChar.length; i++) { 81 if (newChar[i] > 128) { 82 try { 83 pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0); 84 } catch (BadHanyuPinyinOutputFormatCombination e) { 85 e.printStackTrace(); 86 } 87 } else { 88 pinyinStr += newChar[i]; 89 } 90 } 91 return pinyinStr; 92 } 93 94 /** 95 * 將中文字符串轉換成拼音小寫 96 * 97 * @param chinese 98 * @return 99 * @author zfc 100 * 101 */ 102 public static String toPinyinLowCase(String chinese) { 103 if (null == chinese) { 104 return null; 105 } 106 String pinyinStr = ""; 107 // 將字符串轉為字符數組 108 char[] newChar = chinese.toCharArray(); 109 HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); 110 // 設置小寫 111 defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); 112 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); 113 for (int i = 0; i < newChar.length; i++) { 114 if (newChar[i] > 128) { 115 try { 116 pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0]; 117 } catch (BadHanyuPinyinOutputFormatCombination e) { 118 e.printStackTrace(); 119 } 120 } else { 121 pinyinStr += newChar[i]; 122 } 123 } 124 return pinyinStr; 125 } 126 127 /** 128 * 將中文字符串轉換成拼音大寫 129 * 130 * @param chinese 131 * @return 132 * @author zfc 133 * 134 */ 135 public static String toPinyinUpCase(String chinese) { 136 if (null == chinese) { 137 return null; 138 } 139 String pinyinStr = ""; 140 // 將字符串轉為字符數組 141 char[] newChar = chinese.toCharArray(); 142 HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); 143 // 設置大寫 144 defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE); 145 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); 146 for (int i = 0; i < newChar.length; i++) { 147 if (newChar[i] > 128) { 148 try { 149 pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0]; 150 } catch (BadHanyuPinyinOutputFormatCombination e) { 151 e.printStackTrace(); 152 } 153 } else { 154 pinyinStr += newChar[i]; 155 } 156 } 157 return pinyinStr; 158 } 159 }
三、運行結果