工具类代码
用PinyinHelper类,拼音包实现
public class PinYinUtil {
public static String getPyFirstChar(String chStr) {
StringBuilder sb=new StringBuilder();
if(StringUtils.isBlank(chStr)){
return sb.toString();
}
for (int index = 0; index < chStr.length(); index++) {
char ch = chStr.charAt(index);
String charPin = getCharPinyin(ch);
if (charPin == null) {
sb.append(ch);
continue;
}
sb.append(charPin.toUpperCase().charAt(0));
}
return sb.toString();
}
private static String getCharPinyin(char character) {
String retVal = null;
String[] pinyin = PinyinHelper.toHanyuPinyinStringArray(character);
if (pinyin != null) {
retVal = pinyin[0];
}
return retVal;
}
}