1 import net.sourceforge.pinyin4j.PinyinHelper;
2 import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
3 import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
4 import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
5 import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
6
7
8 /**
9 * 汉字转化为拼音的工具类
10 *
11 */
12 public class PinyinTool {
13
14 HanyuPinyinOutputFormat format = null;
15
16 public static enum Type {
17 UPPERCASE, //全部大写
18 LOWERCASE, //全部小写
19 FIRSTUPPER //首字母大写
20 }
21
22 public PinyinTool(){
23 format = new HanyuPinyinOutputFormat();
24 format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
25 format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
26 }
27
28 public PinyinTool(HanyuPinyinCaseType cType,HanyuPinyinToneType tType){
29 format = new HanyuPinyinOutputFormat();
30 format.setCaseType(cType);
31 format.setToneType(tType);
32 }
33
34 public String toPinYin(String str) throws BadHanyuPinyinOutputFormatCombination{
35 return toPinYin(str, "", Type.LOWERCASE);
36 }
37
38 public String toPinYin(String str,Type type) throws BadHanyuPinyinOutputFormatCombination{
39 return toPinYin(str, "", type);
40 }
41
42 public String toPinYin(String str,String spera) throws BadHanyuPinyinOutputFormatCombination{
43 return toPinYin(str, spera, Type.LOWERCASE);
44 }
45
46 /**
47 * 将str转换成拼音,如果不是汉字或者没有对应的拼音,则不作转换
48 * @param str:要转化的汉字
49 * @param spera:转化结果的分割符
50 * @return
51 * @throws BadHanyuPinyinOutputFormatCombination
52 */
53 public String toPinYin(String str, String spera, Type type) throws BadHanyuPinyinOutputFormatCombination {
54 if(str == null || str.trim().length()==0)
55 return "";
56 if(type == Type.UPPERCASE)
57 format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
58 else
59 format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
60
61 String py = "";
62 String temp = "";
63 String[] t;
64 for(int i=0;i<str.length();i++){
65 char c = str.charAt(i);
66 if((int)c <= 128)
67 py += c;
68 else{
69 t = PinyinHelper.toHanyuPinyinStringArray(c, format);
70 if(t == null)
71 py += c;
72 else{
73 temp = t[0];
74 if(type == Type.FIRSTUPPER)
75 temp = t[0].toUpperCase().charAt(0)+temp.substring(1);
76 py += temp+(i==str.length()-1?"":spera);
77 }
78 }
79 }
80 return py.trim();
81 }
82
83 public static void main(String[] args) {
84 try {
85 System.out.println(new PinyinTool().toPinYin("中共中央"));
86 } catch (BadHanyuPinyinOutputFormatCombination e) {
87 // TODO Auto-generated catch block
88 e.printStackTrace();
89 }
90 }
91
92 }
gradle引入:
compile 'com.belerweb:pinyin4j:2.5.0'
maven引入:<dependency><groupId>com.belerweb</groupId><artifactId>pinyin4j</artifactId><version>2.5.0</version></dependency>
jar下载地址:http://mvnrepository.com/artifact/com.belerweb/pinyin4j/2.5.0
1 import net.sourceforge.pinyin4j.PinyinHelper;
2 import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
3 import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
4 import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
5 import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
6 import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
7
8 public class HanyuPinyinHelper {
9
10 /**
11 * 将文字转为汉语拼音
12 * @param chineselanguage 要转成拼音的中文
13 */
14 public String toHanyuPinyin(String ChineseLanguage){
15 char[] cl_chars = ChineseLanguage.trim().toCharArray();
16 String hanyupinyin = "";
17 HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
18 defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 输出拼音全部小写
19 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
20 defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V) ;
21 try {
22 for (int i=0; i<cl_chars.length; i++){
23 if (String.valueOf(cl_chars[i]).matches("[\u4e00-\u9fa5]+")){// 如果字符是中文,则将中文转为汉语拼音
24 hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0];
25 } else {// 如果字符不是中文,则不转换
26 hanyupinyin += cl_chars[i];
27 }
28 }
29 } catch (BadHanyuPinyinOutputFormatCombination e) {
30 System.out.println("字符不能转成汉语拼音");
31 }
32 return hanyupinyin;
33 }
34
35 public static String getFirstLettersUp(String ChineseLanguage){
36 return getFirstLetters(ChineseLanguage ,HanyuPinyinCaseType.UPPERCASE);
37 }
38
39 public static String getFirstLettersLo(String ChineseLanguage){
40 return getFirstLetters(ChineseLanguage ,HanyuPinyinCaseType.LOWERCASE);
41 }
42
43 public static String getFirstLetters(String ChineseLanguage,HanyuPinyinCaseType caseType) {
44 char[] cl_chars = ChineseLanguage.trim().toCharArray();
45 String hanyupinyin = "";
46 HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
47 defaultFormat.setCaseType(caseType);// 输出拼音全部大写
48 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
49 try {
50 for (int i = 0; i < cl_chars.length; i++) {
51 String str = String.valueOf(cl_chars[i]);
52 if (str.matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音,并取第一个字母
53 hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0].substring(0, 1);
54 } else if (str.matches("[0-9]+")) {// 如果字符是数字,取数字
55 hanyupinyin += cl_chars[i];
56 } else if (str.matches("[a-zA-Z]+")) {// 如果字符是字母,取字母
57 hanyupinyin += cl_chars[i];
58 } else {// 否则不转换
59 hanyupinyin += cl_chars[i];//如果是标点符号的话,带着
60 }
61 }
62 } catch (BadHanyuPinyinOutputFormatCombination e) {
63 System.out.println("字符不能转成汉语拼音");
64 }
65 return hanyupinyin;
66 }
67
68 public static String getPinyinString(String ChineseLanguage){
69 char[] cl_chars = ChineseLanguage.trim().toCharArray();
70 String hanyupinyin = "";
71 HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
72 defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 输出拼音全部大写
73 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
74 try {
75 for (int i = 0; i < cl_chars.length; i++) {
76 String str = String.valueOf(cl_chars[i]);
77 if (str.matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音,并取第一个字母
78 hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(
79 cl_chars[i], defaultFormat)[0];
80 } else if (str.matches("[0-9]+")) {// 如果字符是数字,取数字
81 hanyupinyin += cl_chars[i];
82 } else if (str.matches("[a-zA-Z]+")) {// 如果字符是字母,取字母
83
84 hanyupinyin += cl_chars[i];
85 } else {// 否则不转换
86 }
87 }
88 } catch (BadHanyuPinyinOutputFormatCombination e) {
89 System.out.println("字符不能转成汉语拼音");
90 }
91 return hanyupinyin;
92 }
93 /**
94 * 取第一个汉字的第一个字符
95 * @Title: getFirstLetter
96 * @Description: TODO
97 * @return String
98 * @throws
99 */
100 public static String getFirstLetter(String ChineseLanguage){
101 char[] cl_chars = ChineseLanguage.trim().toCharArray();
102 String hanyupinyin = "";
103 HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
104 defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);// 输出拼音全部大写
105 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
106 try {
107 String str = String.valueOf(cl_chars[0]);
108 if (str.matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音,并取第一个字母
109 hanyupinyin = PinyinHelper.toHanyuPinyinStringArray(
110 cl_chars[0], defaultFormat)[0].substring(0, 1);;
111 } else if (str.matches("[0-9]+")) {// 如果字符是数字,取数字
112 hanyupinyin += cl_chars[0];
113 } else if (str.matches("[a-zA-Z]+")) {// 如果字符是字母,取字母
114
115 hanyupinyin += cl_chars[0];
116 } else {// 否则不转换
117
118 }
119 } catch (BadHanyuPinyinOutputFormatCombination e) {
120 System.out.println("字符不能转成汉语拼音");
121 }
122 return hanyupinyin;
123 }
124
125 public static void main(String[] args) {
126 HanyuPinyinHelper hanyuPinyinHelper = new HanyuPinyinHelper() ;
127
128 }
129 }