java 根據(jù)漢字生成拼音全拼或拼音首字母的示例
1.情景展示
java 根據(jù)中文生成對(duì)應(yīng)的拼音
2.準(zhǔn)備工作
所需jar包:pinyin4j-2.5.0.jar
3.解決方案
導(dǎo)包
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;
代碼實(shí)現(xiàn)
/** * 根據(jù)漢字生成拼音全拼或拼音首字母 * @explain * @author Marydon * @creationTime 2020年5月14日下午4:26:30 * @version 1.0 * @since * @email marydon20170307@163.com */ public class GetPinyin { /** * 得到全拼 * @param str * @return 全拼(小寫) */ public static String getPinYin(String str){ char t1[]=null; t1=str.toCharArray(); String[] t2=new String[t1.length]; HanyuPinyinOutputFormat t3=new HanyuPinyinOutputFormat(); t3.setCaseType(HanyuPinyinCaseType.LOWERCASE); t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE); t3.setVCharType(HanyuPinyinVCharType.WITH_V); String t4=""; int t0=t1.length; try { for ( int i = 0; i < t0; i++ ) { //是用來(lái)判斷是不是中文的一個(gè)條件,采用的是unicode編碼 if(Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")){ t2= PinyinHelper.toHanyuPinyinStringArray(t1[i],t3); t4+=t2[0]; }else { t4+=Character.toString(t1[i]); } } return t4; } catch ( BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination ) { badHanyuPinyinOutputFormatCombination.printStackTrace(); } return t4; } /** * 得到漢字首字母的拼音 * @param str * @return 拼音首字母(大寫) */ public static String getPinYinHeaderChar(String str){ String convert=""; for ( int i = 0; i < str.length(); i++ ) { char word=str.charAt(i); String[] pinYinArray=PinyinHelper.toHanyuPinyinStringArray(word); if ( pinYinArray!=null ){ convert+=pinYinArray[0].charAt(0); }else { convert+=word; } } return convert.toUpperCase(); } //測(cè)試 public static void main(String[] args) { System.out.println(getPinYin("火影忍者M(jìn)arydon")); System.out.println(getPinYinHeaderChar("海賊王")); } }
4.效果展示
以上就是java 根據(jù)漢字生成拼音全拼或拼音首字母的示例的詳細(xì)內(nèi)容,更多關(guān)于Java 漢字生成拼音的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java中構(gòu)造方法和普通方法的區(qū)別說(shuō)明
這篇文章主要介紹了java中構(gòu)造方法和普通方法的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08Java調(diào)用打印機(jī)的2種方式舉例(無(wú)驅(qū)/有驅(qū))
我們平時(shí)使用某些軟件或者在超市購(gòu)物的時(shí)候都會(huì)發(fā)現(xiàn)可以使用打印機(jī)進(jìn)行打印,這篇文章主要給大家介紹了關(guān)于Java調(diào)用打印機(jī)的2種方式,分別是無(wú)驅(qū)/有驅(qū)的相關(guān)資料,需要的朋友可以參考下2023-11-11SpringBoot中fastjson自定義序列化和反序列化的實(shí)戰(zhàn)分享
在fastjson庫(kù)中,為了提供靈活的序列化和反序列化機(jī)制,設(shè)計(jì)了一系列的擴(kuò)展點(diǎn),以下是在SpringBoot和SpringClould環(huán)境中對(duì)這些擴(kuò)展點(diǎn)的詳細(xì)介紹及其實(shí)戰(zhàn)使用,通過(guò)代碼示例講解的非常詳細(xì),需要的朋友可以參考下2024-07-07JAVA WSIMPORT生成WEBSERVICE客戶端401認(rèn)證過(guò)程圖解
這篇文章主要介紹了JAVA WSIMPORT生成WEBSERVICE客戶端401認(rèn)證過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10Spring配置多個(gè)數(shù)據(jù)源并實(shí)現(xiàn)動(dòng)態(tài)切換示例
本篇文章主要介紹了Spring配置多個(gè)數(shù)據(jù)源并實(shí)現(xiàn)動(dòng)態(tài)切換示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04