将pinyin4j-2.5.0.jar (附件中有,记得解压)加入到classpath中。
工具类PinyinUtils代码见下:
package com.test.utils;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
public class PinyinUtils {
/**
* 判断字符串是否为空
*
* @param text
* 字符串
* @return
*/
private static boolean isNULL(String text) {
if (text == null || text.trim().equals("")
|| "null".equalsIgnoreCase(text)) {
return true;
}
return false;
}
/**
* 获取汉字串拼音首字母,英文字符不变
*
* @param chineseString
* 汉字串
* @return 汉语拼音首字母
*/
public static String getFirstSpell(String chineseString) {
StringBuffer pybf = new StringBuffer();
if (isNULL(chineseString)) {
return null;
}
char arr = chineseString.trim().replaceAll(":", "").replaceAll("(", "")
.replaceAll(")", "").toCharArray()[0];
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
if (arr > 128) {
try {
String[] _t = PinyinHelper.toHanyuPinyinStringArray(arr,
defaultFormat);
if (_t != null) {
pybf.append(_t[0].charAt(0));
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pybf.append(arr);
}
String result = pybf.toString().replaceAll("\\W", "").trim();
return result.toUpperCase();
}
/**
* 获取汉字串拼音,英文字符不变
*
* @param chineseString
* 汉字串
* @return 汉语拼音
*/
public static String getSpell(String chineseString) {
StringBuffer pybf = new StringBuffer();
if (isNULL(chineseString)) {
return null;
}
char[] arr = chineseString.trim().toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
try {
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 128) {
try {
String[] pinyinArray = PinyinHelper
.toHanyuPinyinStringArray(arr[i], defaultFormat);
if (pinyinArray != null && pinyinArray.length > 0) {
String firstPinyin = pinyinArray[0];
if (firstPinyin != null) {
pybf.append(firstPinyin);
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pybf.append(arr[i]);
}
}
return pybf.toString();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
/**
* 取中文串每个字的首字母
*
* @param chineseString
* 中国
* @return ZG
*/
public static String getAllFirstSpell(String chineseString) {
StringBuffer pybf = new StringBuffer();
if (isNULL(chineseString)) {
return null;
}
char[] arr = chineseString.trim().toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
try {
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 128) {
String han = "" + arr[i];
pybf.append(getFirstSpell(han));
}
}
return pybf.toString();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
public static void main(String[] args) {
System.out.println(getFirstSpell("C"));
System.out.println(getAllFirstSpell("中国"));
}
}
转载于:https://blog.51cto.com/12324480/1875402