import net.sourceforge.pinyin4j.PinyinHelper; /** * * @Title: getPinYinHeadChar * @Description: TODO 获取中文的首字母 * @param str 传入的中文参数 * @return * @return: String */ public static String getPinYinHeadChar(String str) { String convert = ""; for (int j = 0; j < str.length(); j++) { char word = str.charAt(j); String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word); if (pinyinArray != null) { convert += pinyinArray[0].charAt(0); } else { convert += word; } } return convert; }
测试:
System.out.println(PinYinUtil.getPinYinHeadChar("我比较"));===》wbj
时间: 2024-10-28 21:19:34