需要替换判断字符串为空的部分代码
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;
/**
*
* 汉字和拼音字母转换的工具类(pinyin4j)
*
*/
public class MemberNameChange {
/**
* 汉字转换位汉语拼音首字母,英文字符不变
*
* @param chines 汉字
* @return 拼音
*/
public static String converterToFirstSpell(String chines) {
String pinyinName = "";
char[] nameChar = chines.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
StringBuffer buff = new StringBuffer();
for (int i = 0; i < nameChar.length; i++) {
if (nameChar[i] > 128) {
try {
String[] pyArr = PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat);
if(pyArr != null){
String s = pyArr[0];
if(!StringTool.isEmpty(s)){
nameChar[i] = s.charAt(0);
buff.append(nameChar[i]);
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
buff.append(nameChar[i]);
}
}
pinyinName = buff.toString();
return pinyinName;
}
}