package com.mm.util.py4j;
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.HanyuPinyinVCharType;
/**
* 汉字转拼音工具类
* @author mm
*
*/
public class SpellHelper {
/**
* 将汉字转换为拼音
*
* @param chinese
* 汉字
* @return 拼音
*/
public static String getChineseSpell(String chinese) {
HanyuPinyinOutputFormat pyFormat = new HanyuPinyinOutputFormat();
// 设置输出格式为大写或者小写
pyFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
// 设置是否输出音调
pyFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
pyFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
return PinyinHelper.toHanyuPinyinString(chinese, pyFormat, " ");
}
/**
* 将姓名的第一个字母大写
*
* @param name
* 姓名
* @return 第一个字母大写的拼音
*/
public static String getUpEname(String name) {
char[] strs = name.toCharArray();
String newName = null;
// 名字的长度
if (strs.length == 2) {
newName = toUpCase(getChineseSpell("" + strs[0])) + " "
+ toUpCase(getChineseSpell("" + strs[1]));
} else if (strs.length == 3) {
newName = toUpCase(getChineseSpell("" + strs[0])) + " "
+ toUpCase(getChineseSpell("" + strs[1] + strs[2]));
} else if (strs.length == 4) {
newName = toUpCase(getChineseSpell("" + strs[0] + strs[1])) + " "
+ toUpCase(getChineseSpell("" + strs[2] + strs[3]));
} else {
newName = toUpCase(getChineseSpell(name));
}
return newName;
}
/**
* 将字符首字母大写
*
* @param str
* @return
*/
private static String toUpCase(String str) {
StringBuffer newstr = new StringBuffer();
newstr.append((str.substring(0, 1)).toUpperCase()).append(
str.substring(1, str.length()));
return newstr.toString();
}
public static void main(String[] args) {
System.out.println(getUpEname("冯伟"));
}
}
汉字转拼音工具类
最新推荐文章于 2021-04-08 17:59:02 发布