java汉字怎么获取首字母_java根据汉字获取全拼和首字母

importnet.sourceforge.pinyin4j.PinyinHelper;importnet.sourceforge.pinyin4j.format.HanyuPinyinCaseType;importnet.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;importnet.sourceforge.pinyin4j.format.HanyuPinyinToneType;importnet.sourceforge.pinyin4j.format.HanyuPinyinVCharType;importnet.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;public classPinyin {/*** 得到 全拼

*

*@paramsrc

*@return

*/

public staticString getPingYin(String src) {char[] t1 = null;

t1=src.toCharArray();

String[] t2= newString[t1.length];

HanyuPinyinOutputFormat t3= newHanyuPinyinOutputFormat();

t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);

t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

t3.setVCharType(HanyuPinyinVCharType.WITH_V);

String t4= "";int t0 =t1.length;try{for (int i = 0; i < t0; i++) {//判断是否为汉字字符

if (java.lang.Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {

t2=PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);

t4+= t2[0];

}else{

t4+=java.lang.Character.toString(t1[i]);

}

}returnt4;

}catch(BadHanyuPinyinOutputFormatCombination e1) {

e1.printStackTrace();

}returnt4;

}/*** 得到中文首字母

*

*@paramstr

*@return

*/

public staticString 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;

}

}returnconvert;

}/*** 将字符串转移为ASCII码

*

*@paramcnStr

*@return

*/

public staticString getCnASCII(String cnStr) {

StringBuffer strBuf= newStringBuffer();byte[] bGBK =cnStr.getBytes();for (int i = 0; i < bGBK.length; i++) {//System.out.println(Integer.toHexString(bGBK[i]&0xff));

strBuf.append(Integer.toHexString(bGBK[i] & 0xff));

}returnstrBuf.toString();

}public static voidmain(String[] args) {

String cnStr= "凌";

System.out.println(getPingYin(cnStr));

System.out.println(getPinYinHeadChar(cnStr));

}

}

你可能感兴趣的:(java汉字怎么获取首字母)