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 SpellHelper {
//将中文转换为英文
public static String getEname(String name) throws BadHanyuPinyinOutputFormatCombination
{
HanyuPinyinOutputFormat pyFormat = new HanyuPinyinOutputFormat();
pyFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
pyFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
pyFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
return PinyinHelper.toHanyuPinyinString(name, pyFormat, "");
}
//姓、名的第一个字母需要为大写
public static String getUpEname(String name) throws BadHanyuPinyinOutputFormatCombination {
char[] strs = name.toCharArray();
String newname = null;
//名字的长度
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) throws BadHanyuPinyinOutputFormatCombination {
System.out.println(getUpEname("李宇春"));
}
}
<!-- https://mvnrepository.com/artifact/com.belerweb/pinyin4j -->
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
</dependency>
Java中文转英文汉字转拼音
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 一、前言 工作中有时候会遇到汉字拼音转换的需求,例如:用户首字母搜索某个内容的时候,wzry 可搜索 王者荣耀相关...
- 一、权杖侍从—行动、身体、事业方面: 感想:抽到这张牌不太喜欢,怎么又是权杖侍从?不喜欢这个侍从很low的状态; ...
- 书中提及传统的品牌路径是先砸知名度,再做美誉度,最后是维护忠诚度。互联网企业的品牌路径恰恰相反。但好像现实中并非...