<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
</dependency>
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.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class HanyuPinyinUtil {
private Log LOG = LogFactory.getLog(HanyuPinyinUtil.class);
public String toHanyuPinyin(String ChineseLanguage){
char[] cl_chars = ChineseLanguage.trim().toCharArray();
String hanyupinyin = "";
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V) ;
try {
for (int i=0; i<cl_chars.length; i++){
if (String.valueOf(cl_chars[i]).matches("[\u4e00-\u9fa5]+")){
hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0];
} else {
hanyupinyin += cl_chars[i];
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
LOG.info("字符不能转成汉语拼音");
}
return hanyupinyin;
}
public String getFirstLettersUp(String ChineseLanguage){
return getFirstLetters(ChineseLanguage ,HanyuPinyinCaseType.UPPERCASE);
}
public String getFirstLettersLo(String ChineseLanguage){
return getFirstLetters(ChineseLanguage ,HanyuPinyinCaseType.LOWERCASE);
}
public String getFirstLetters(String ChineseLanguage,HanyuPinyinCaseType caseType) {
char[] cl_chars = ChineseLanguage.trim().toCharArray();
String hanyupinyin = "";
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(caseType);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
try {
for (int i = 0; i < cl_chars.length; i++) {
String str = String.valueOf(cl_chars[i]);
if (str.matches("[\u4e00-\u9fa5]+")) {
hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0].substring(0, 1);
} else if (str.matches("[0-9]+")) {
hanyupinyin += cl_chars[i];
} else if (str.matches("[a-zA-Z]+")) {
hanyupinyin += cl_chars[i];
} else {
hanyupinyin += cl_chars[i];
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
LOG.error("字符不能转成汉语拼音");
}
return hanyupinyin;
}
public String getPinyinString(String ChineseLanguage){
char[] cl_chars = ChineseLanguage.trim().toCharArray();
String hanyupinyin = "";
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
try {
for (int i = 0; i < cl_chars.length; i++) {
String str = String.valueOf(cl_chars[i]);
if (str.matches("[\u4e00-\u9fa5]+")) {
hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(
cl_chars[i], defaultFormat)[0];
} else if (str.matches("[0-9]+")) {
hanyupinyin += cl_chars[i];
} else if (str.matches("[a-zA-Z]+")) {
hanyupinyin += cl_chars[i];
} else {
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
LOG.error("字符不能转成汉语拼音");
}
return hanyupinyin;
}
public String getFirstLetter(String ChineseLanguage){
char[] cl_chars = ChineseLanguage.trim().toCharArray();
String hanyupinyin = "";
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
try {
String str = String.valueOf(cl_chars[0]);
if (str.matches("[\u4e00-\u9fa5]+")) {
hanyupinyin = PinyinHelper.toHanyuPinyinStringArray(
cl_chars[0], defaultFormat)[0].substring(0, 1);;
} else if (str.matches("[0-9]+")) {
hanyupinyin += cl_chars[0];
} else if (str.matches("[a-zA-Z]+")) {
hanyupinyin += cl_chars[0];
} else {
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
LOG.error("字符不能转成汉语拼音");
}
return hanyupinyin;
}
public static void main(String[] args) {
HanyuPinyinUtil hanyuPinyinUtil = new HanyuPinyinUtil();
System.out.println(hanyuPinyinUtil.toHanyuPinyin("测试汉语字符串"));
}
}