1. <dependency>
    2. <groupId>com.belerweb</groupId>
    3. <artifactId>pinyin4j</artifactId>
    4. <version>2.5.0</version>
    5. </dependency>
    1. package com.infinitePossibilities.utils;
    2. import java.util.Arrays;
    3. import java.util.HashSet;
    4. import java.util.Set;
    5. import net.sourceforge.pinyin4j.PinyinHelper;
    6. import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
    7. import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
    8. import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
    9. import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
    10. import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
    11. import wfc.service.log.Log;
    12. public class pinYinUtils {
    13. public static int wordLength = 20;
    14. /**
    15. * 将字符串中的中文转化为拼音,其他字符不变
    16. *
    17. * @param inputString
    18. * @return
    19. */
    20. public static String getPingYin(String inputString,int type) {
    21. HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
    22. //大小写
    23. if(1 == type){
    24. format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    25. }else{
    26. format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
    27. }
    28. format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    29. format.setVCharType(HanyuPinyinVCharType.WITH_V);
    30. char[] input = inputString.trim().toCharArray();
    31. String output = "";
    32. try {
    33. for (int i = 0; i < input.length; i++) {
    34. if (Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")) {
    35. String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);
    36. output += temp[0];
    37. } else {
    38. output += Character.toString(input[i]);
    39. }
    40. }
    41. } catch (BadHanyuPinyinOutputFormatCombination e) {
    42. e.printStackTrace();
    43. }
    44. return output;
    45. }
    46. /**
    47. * 获取汉字串拼音首字母,英文字符不变
    48. * @param chinese 汉字串
    49. * @return 汉语拼音首字母
    50. */
    51. public static String getFirstSpell(String chinese,int type) {
    52. StringBuffer pybf = new StringBuffer();
    53. char[] arr = chinese.toCharArray();
    54. HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
    55. //大小写
    56. if(1 == type){
    57. defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    58. }else{
    59. defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
    60. }
    61. defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    62. for (int i = 0; i < arr.length; i++) {
    63. if (arr[i] > 128) {
    64. try {
    65. String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
    66. if (temp != null) {
    67. pybf.append(temp[0].charAt(0));
    68. }
    69. } catch (BadHanyuPinyinOutputFormatCombination e) {
    70. e.printStackTrace();
    71. }
    72. } else {
    73. pybf.append(arr[i]);
    74. }
    75. }
    76. return pybf.toString().replaceAll("\\W", "").trim();
    77. }
    78. /**
    79. * 获取拼音集合
    80. *
    81. * @param src
    82. * @return Set<String>
    83. */
    84. public static Set<String> getPinyin(String src) {
    85. if (src != null && !src.trim().equalsIgnoreCase("")) {
    86. char[] srcChar;
    87. srcChar = src.toCharArray();
    88. // 汉语拼音格式输出类
    89. HanyuPinyinOutputFormat hanYuPinOutputFormat = new HanyuPinyinOutputFormat();
    90. // 输出设置,大小写,音标方式等
    91. hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    92. hanYuPinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    93. hanYuPinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
    94. // 因为多音字问题,导致在Exchange方法中排序组合后内存溢出,所以根据使用习惯或者查询喜欢,只取前几个文字,解决内存溢出问题
    95. if (src.length() <= wordLength) {
    96. wordLength = src.length();
    97. }
    98. String[][] temp = new String[wordLength][];
    99. for (int i = 0; i < wordLength; i++) {
    100. char c = srcChar[i];
    101. // 是中文或者a-z或者A-Z转换拼音(我的需求,是保留中文或者a-z或者A-Z)
    102. if (String.valueOf(c).matches("[\\u4E00-\\u9FA5]+")) {
    103. try {
    104. temp[i] = PinyinHelper.toHanyuPinyinStringArray(
    105. srcChar[i], hanYuPinOutputFormat);
    106. } catch (BadHanyuPinyinOutputFormatCombination e) {
    107. e.printStackTrace();
    108. }
    109. } else if (((int) c >= 65 && (int) c <= 90) || ((int) c >= 97 && (int) c <= 122)) {
    110. temp[i] = new String[] { String.valueOf(srcChar[i]) };
    111. } else {
    112. temp[i] = new String[] { "" };
    113. }
    114. }
    115. System.out.println(Arrays.toString(temp));
    116. String[] pingyinArray = Exchange(temp);
    117. Set<String> pinyinSet = new HashSet<String>();
    118. for (int i = 0; i < pingyinArray.length; i++) {
    119. pinyinSet.add(pingyinArray[i]);
    120. }
    121. return pinyinSet;
    122. }
    123. Log.info("参数校验不合法!");
    124. return new HashSet<String>();
    125. }
    126. public static void main(String[] args) {
    127. System.out.println(getPingYin("将字符串中的中文转化为拼音,其他字符不变",1));
    128. System.out.println(getFirstSpell("获取汉字串拼音首字母,英文字符不变",1));
    129. System.out.println(getPinyin("获取拼音集合11测试cc"));
    130. }
    131. /**
    132. * 递归
    133. *
    134. * @param strJaggedArray
    135. * @return
    136. */
    137. public static String[] Exchange(String[][] strJaggedArray) {
    138. String[][] temp = DoExchange(strJaggedArray);
    139. System.out.println("---:"+ Arrays.toString(temp));
    140. return temp[0];
    141. }
    142. /**
    143. * 递归
    144. *
    145. * @param strJaggedArray
    146. * @return
    147. */
    148. private static String[][] DoExchange(String[][] strJaggedArray) {
    149. int len = strJaggedArray.length;
    150. if (len >= 2) {
    151. int len1 = strJaggedArray[0].length;
    152. int len2 = strJaggedArray[1].length;
    153. int newlen = len1 * len2;
    154. String[] temp = new String[newlen];
    155. int Index = 0;
    156. for (int i = 0; i < len1; i++) {
    157. for (int j = 0; j < len2; j++) {
    158. temp[Index] = strJaggedArray[0][i] + strJaggedArray[1][j];
    159. Index++;
    160. }
    161. }
    162. String[][] newArray = new String[len - 1][];
    163. for (int i = 2; i < len; i++) {
    164. newArray[i - 1] = strJaggedArray[i];
    165. }
    166. newArray[0] = temp;
    167. return DoExchange(newArray);
    168. } else {
    169. return strJaggedArray;
    170. }
    171. }
    172. }