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 {
//將中文轉(zhuǎn)換為英文
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中文轉(zhuǎn)英文漢字轉(zhuǎn)拼音
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
- 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 一、前言 工作中有時候會遇到漢字拼音轉(zhuǎn)換的需求,例如:用戶首字母搜索某個內(nèi)容的時候,wzry 可搜索 王者榮耀相關(guān)...
- 一、權(quán)杖侍從—行動、身體、事業(yè)方面: 感想:抽到這張牌不太喜歡,怎么又是權(quán)杖侍從?不喜歡這個侍從很low的狀態(tài); ...
- 書中提及傳統(tǒng)的品牌路徑是先砸知名度,再做美譽度,最后是維護忠誠度。互聯(lián)網(wǎng)企業(yè)的品牌路徑恰恰相反。但好像現(xiàn)實中并非...