Commit b3d847e402006235bca96346ed671c67bb3a7e15
1 parent
77bc44572a
Exists in
master
and in
3 other branches
code update
Showing 1 changed file with 104 additions and 0 deletions
platform-common/src/main/java/com/lyms/platform/common/utils/PingYinUtil.java
View file @
b3d847e
| 1 | +package com.lyms.platform.common.utils; | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * Created by Administrator on 2016/9/6. | |
| 5 | + */ | |
| 6 | +import net.sourceforge.pinyin4j.PinyinHelper; | |
| 7 | +import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; | |
| 8 | +import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; | |
| 9 | +import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; | |
| 10 | +import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType; | |
| 11 | +import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * 拼音工具类 | |
| 15 | + * | |
| 16 | + * @author lsf | |
| 17 | + */ | |
| 18 | +public class PingYinUtil { | |
| 19 | + /** | |
| 20 | + * 将字符串中的中文转化为拼音,其他字符不变 | |
| 21 | + * | |
| 22 | + * @param inputString | |
| 23 | + * @return | |
| 24 | + */ | |
| 25 | + public static String getPingYin(String inputString) { | |
| 26 | + HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); | |
| 27 | + format.setCaseType(HanyuPinyinCaseType.LOWERCASE); | |
| 28 | + format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); | |
| 29 | + format.setVCharType(HanyuPinyinVCharType.WITH_V); | |
| 30 | + | |
| 31 | + char[] input = inputString.trim().toCharArray(); | |
| 32 | + String output = ""; | |
| 33 | + | |
| 34 | + try { | |
| 35 | + for (int i = 0; i < input.length; i++) { | |
| 36 | + if (java.lang.Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")) { | |
| 37 | + String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format); | |
| 38 | + output += temp[0]; | |
| 39 | + } else | |
| 40 | + output += java.lang.Character.toString(input[i]); | |
| 41 | + } | |
| 42 | + } catch (BadHanyuPinyinOutputFormatCombination e) { | |
| 43 | + e.printStackTrace(); | |
| 44 | + } | |
| 45 | + return output; | |
| 46 | + } | |
| 47 | + /** | |
| 48 | + * 获取汉字串拼音首字母,英文字符不变 | |
| 49 | + * @param chinese 汉字串 | |
| 50 | + * @return 汉语拼音首字母 | |
| 51 | + */ | |
| 52 | + public static String getFirstSpell(String chinese) { | |
| 53 | + StringBuffer pybf = new StringBuffer(); | |
| 54 | + char[] arr = chinese.toCharArray(); | |
| 55 | + HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); | |
| 56 | + defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); | |
| 57 | + defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); | |
| 58 | + for (int i = 0; i < arr.length; i++) { | |
| 59 | + if (arr[i] > 128) { | |
| 60 | + try { | |
| 61 | + String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat); | |
| 62 | + if (temp != null) { | |
| 63 | + pybf.append(temp[0].charAt(0)); | |
| 64 | + } | |
| 65 | + } catch (BadHanyuPinyinOutputFormatCombination e) { | |
| 66 | + e.printStackTrace(); | |
| 67 | + } | |
| 68 | + } else { | |
| 69 | + pybf.append(arr[i]); | |
| 70 | + } | |
| 71 | + } | |
| 72 | + return pybf.toString().replaceAll("\\W", "").trim(); | |
| 73 | + } | |
| 74 | + /** | |
| 75 | + * 获取汉字串拼音,英文字符不变 | |
| 76 | + * @param chinese 汉字串 | |
| 77 | + * @return 汉语拼音 | |
| 78 | + */ | |
| 79 | + public static String getFullSpell(String chinese) { | |
| 80 | + StringBuffer pybf = new StringBuffer(); | |
| 81 | + char[] arr = chinese.toCharArray(); | |
| 82 | + HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); | |
| 83 | + defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); | |
| 84 | + defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); | |
| 85 | + for (int i = 0; i < arr.length; i++) { | |
| 86 | + if (arr[i] > 128) { | |
| 87 | + try { | |
| 88 | + pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]); | |
| 89 | + } catch (BadHanyuPinyinOutputFormatCombination e) { | |
| 90 | + e.printStackTrace(); | |
| 91 | + } | |
| 92 | + } else { | |
| 93 | + pybf.append(arr[i]); | |
| 94 | + } | |
| 95 | + } | |
| 96 | + return pybf.toString(); | |
| 97 | + } | |
| 98 | + | |
| 99 | + public static void main(String[] args) | |
| 100 | + { | |
| 101 | + String s = getFirstSpell("中华人民共和国"); | |
| 102 | + System.out.print(s); | |
| 103 | + } | |
| 104 | +} |