汉子拼音转换类 @author SeanDragon
| 14 | * @author SeanDragon |
| 15 | */ |
| 16 | public final class ToolPinYin { |
| 17 | |
| 18 | private ToolPinYin() { |
| 19 | throw new UnsupportedOperationException("我是工具类,别初始化我。。。"); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * 得到 全拼 |
| 24 | * |
| 25 | * @param src |
| 26 | * |
| 27 | * @return |
| 28 | */ |
| 29 | public static String getPinYin(String src) throws BadHanyuPinyinOutputFormatCombination { |
| 30 | char[] t1; |
| 31 | t1 = src.toCharArray(); |
| 32 | String[] t2; |
| 33 | HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat(); |
| 34 | t3.setCaseType(HanyuPinyinCaseType.LOWERCASE); |
| 35 | t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE); |
| 36 | t3.setVCharType(HanyuPinyinVCharType.WITH_V); |
| 37 | StringBuilder t4 = new StringBuilder(); |
| 38 | for (char aT1 : t1) { |
| 39 | // 判断是否为汉字字符 |
| 40 | if (Character.toString(aT1).matches("[\\u4E00-\\u9FA5]+")) { |
| 41 | t2 = PinyinHelper.toHanyuPinyinStringArray(aT1, t3); |
| 42 | t4.append(t2[0]); |
| 43 | } else { |
| 44 | t4.append(aT1); |
| 45 | } |
| 46 | } |
| 47 | return t4.toString(); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * 得到中文首字母 |
| 52 | * |
| 53 | * @param str |
| 54 | * |
| 55 | * @return |
| 56 | */ |
| 57 | public static String getPinYinHeadChar(String str) { |
| 58 | |
| 59 | StringBuilder convert = new StringBuilder(); |
| 60 | for (int j = 0; j < str.length(); j++) { |
| 61 | char word = str.charAt(j); |
| 62 | String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word); |
| 63 | if (pinyinArray != null) { |
| 64 | convert.append(pinyinArray[0].charAt(0)); |
| 65 | } else { |
| 66 | convert.append(word); |
| 67 | } |
| 68 | } |
| 69 | return convert.toString(); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * 将字符串转移为ASCII码 |
nothing calls this directly
no outgoing calls
no test coverage detected