package test;

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;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class PinYin {
	// 将中文转换为英文
	public static String getEname(String name) {
		HanyuPinyinOutputFormat pyFormat = new HanyuPinyinOutputFormat();
		pyFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		pyFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		pyFormat.setVCharType(HanyuPinyinVCharType.WITH_V);

		try {
			return PinyinHelper.toHanyuPinyinString(name, pyFormat, "");
		} catch (BadHanyuPinyinOutputFormatCombination e) {
			e.printStackTrace();
		}
		return null;
	}

	// 姓、名的第一个字母需要为大写
	public static String getUpEname(String name) {
		char[] strs = name.toCharArray();
		String newname = null;

		// 名字的长度 www.2cto.com
		if (strs.length == 2) {
			newname = toUpCase(getEname("" + strs[0])) + " "
					+ toUpCase(getEname("" + strs[1]));
		} else if (strs.length == 3) {
			newname = toUpCase(getEname("" + strs[0])) + " "
					+ toUpCase(getEname("" + strs[1] + strs[2]));
		} else if (strs.length == 4) {
			newname = toUpCase(getEname("" + strs[0] + strs[1])) + " "
					+ toUpCase(getEname("" + strs[2] + strs[3]));
		} else {
			newname = toUpCase(getEname(name));
		}

		return newname;
	}

	// 首字母大写
	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("李宇春"));

	}
}
最近下载更多
wangtao1111111111  LV15 2022年3月7日
reabin  LV2 2022年3月4日
kkk963  LV1 2021年12月20日
heqian  LV16 2021年10月11日
zhangyisheng  LV4 2021年9月24日
openYourAss  LV1 2021年6月7日
asdsasddas  LV6 2021年6月4日
有所改变  LV1 2021年5月31日
liuzhipeng999  LV1 2021年3月27日
jinzhao123  LV1 2021年3月9日
最近浏览更多
wangtao1111111111  LV15 2022年3月7日
reabin  LV2 2022年3月4日
kkk963  LV1 2021年12月20日
nothing 2021年11月22日
暂无贡献等级
heqian  LV16 2021年10月11日
zhangyisheng  LV4 2021年9月24日
爱敲代码的小志 2021年8月28日
暂无贡献等级
openYourAss  LV1 2021年6月7日
asdsasddas  LV6 2021年6月4日
有所改变  LV1 2021年5月31日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友