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;
public class Test {
/**
* 提取汉字的首字母。假设里面含有费中文字符则忽略之;假设全为非中文则返回""。
* @param caseType 当为1时获取的首字母为小写,否则为大写。
* @author 高焕杰
*/
public static String getPinYinHeadChar(String zn_str, int caseType) {
if(zn_str != null && !zn_str.trim().equalsIgnoreCase("")) {
char[] strChar = zn_str.toCharArray();
// 汉语拼音格式输出类
HanyuPinyinOutputFormat hanYuPinOutputFormat = new HanyuPinyinOutputFormat();
// 输出设置。大写和小写,音标方式等
if(1 == caseType) {
hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
} else {
hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
}
hanYuPinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
hanYuPinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
StringBuffer pyStringBuffer = new StringBuffer();
for(int i=0; i<strChar.length; i++) {
char c = strChar[i];
char pyc = strChar[i];
if(String.valueOf(c).matches("[\\u4E00-\\u9FA5]+")) {//是中文或者a-z或者A-Z转换拼音
try {
String[] pyStirngArray = PinyinHelper.toHanyuPinyinStringArray(strChar[i], hanYuPinOutputFormat);
if(null != pyStirngArray && pyStirngArray[0]!=null) {
pyc = pyStirngArray[0].charAt(0);
pyStringBuffer.append(pyc);
}
} catch(BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
}
}
return pyStringBuffer.toString();
}
return null;
}
public static void main(String[] args) {
String cnStr = "刘菲菲";
System.out.println(getPinYinHeadChar(cnStr, 1)); //输出lff
System.out.println(getPinYinHeadChar(cnStr, 2)); //输出LFF
}
}
【0分下载相关jar包】