获取中文拼音(如:广东省 -->guangdongsheng)
/**
* 得到中文全拼
* @param src 需要转化的中文字符串
* @return
*/
public static String getPingYin(String src)
{
char[] t1 = null;
t1 = src.toCharArray();
String[] t2 = new String[t1.length];
HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
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]);
}
}
return t4;
} catch (BadHanyuPinyinOutputFormatCombination e1)
{
e1.printStackTrace();
}
return t4;
}
获取中文首字母缩写(如:广东省-->gds)
/**
* 得到中文首字母
* @param str 需要转化的中文字符串
* @return
*/
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;
}
获取中文首字母并把转化为大写字母(如:广东省--> G)
String s = getPinYinHeadChar("广东省");
System.out.println("得到拼音首字母缩写为:" + s);
StringBuffer sb = new StringBuffer(s);
if (sb.length() > 1)
{
String ss = sb.delete(1, sb.length()).toString();
System.out.println("得到的首字母为:"
+ Character.toUpperCase(ss.toCharArray()[0]) + "");
}
参考资料:http://blog.csdn.net/u013043346/article/details/50131341