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, "");
}
//姓、名的第一個(gè)字母需要為大寫
public static String getUpEname(String name) throws BadHanyuPinyinOutputFormatCombination {
char[] strs = name.toCharArray();
String newname = null;
//名字的長(zhǎng)度
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)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 一、前言 工作中有時(shí)候會(huì)遇到漢字拼音轉(zhuǎn)換的需求,例如:用戶首字母搜索某個(gè)內(nèi)容的時(shí)候,wzry 可搜索 王者榮耀相關(guān)...
- 一、權(quán)杖侍從—行動(dòng)、身體、事業(yè)方面: 感想:抽到這張牌不太喜歡,怎么又是權(quán)杖侍從?不喜歡這個(gè)侍從很low的狀態(tài); ...
- 書中提及傳統(tǒng)的品牌路徑是先砸知名度,再做美譽(yù)度,最后是維護(hù)忠誠(chéng)度?;ヂ?lián)網(wǎng)企業(yè)的品牌路徑恰恰相反。但好像現(xiàn)實(shí)中并非...