Java漢字轉(zhuǎn)成漢語(yǔ)拼音工具類
Java漢字轉(zhuǎn)成漢語(yǔ)拼音工具類,需要用到pinyin4j.jar包.
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 HanyuPinyinHelper { /** * 將文字轉(zhuǎn)為漢語(yǔ)拼音 * @param chineselanguage 要轉(zhuǎn)成拼音的中文 */ public String toHanyuPinyin(String ChineseLanguage){ char[] cl_chars = ChineseLanguage.trim().toCharArray(); String hanyupinyin = ""; HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 輸出拼音全部小寫 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不帶聲調(diào) defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V) ; try { for (int i=0; i<cl_chars.length; i++){ if (String.valueOf(cl_chars[i]).matches("[\u4e00-\u9fa5]+")){// 如果字符是中文,則將中文轉(zhuǎn)為漢語(yǔ)拼音 hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0]; } else {// 如果字符不是中文,則不轉(zhuǎn)換 hanyupinyin += cl_chars[i]; } } } catch (BadHanyuPinyinOutputFormatCombination e) { System.out.println("字符不能轉(zhuǎn)成漢語(yǔ)拼音"); } return hanyupinyin; } public static String getFirstLettersUp(String ChineseLanguage){ return getFirstLetters(ChineseLanguage ,HanyuPinyinCaseType.UPPERCASE); } public static String getFirstLettersLo(String ChineseLanguage){ return getFirstLetters(ChineseLanguage ,HanyuPinyinCaseType.LOWERCASE); } public static String getFirstLetters(String ChineseLanguage,HanyuPinyinCaseType caseType) { char[] cl_chars = ChineseLanguage.trim().toCharArray(); String hanyupinyin = ""; HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); defaultFormat.setCaseType(caseType);// 輸出拼音全部大寫 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不帶聲調(diào) try { for (int i = 0; i < cl_chars.length; i++) { String str = String.valueOf(cl_chars[i]); if (str.matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,則將中文轉(zhuǎn)為漢語(yǔ)拼音,并取第一個(gè)字母 hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0].substring(0, 1); } else if (str.matches("[0-9]+")) {// 如果字符是數(shù)字,取數(shù)字 hanyupinyin += cl_chars[i]; } else if (str.matches("[a-zA-Z]+")) {// 如果字符是字母,取字母 hanyupinyin += cl_chars[i]; } else {// 否則不轉(zhuǎn)換 hanyupinyin += cl_chars[i];//如果是標(biāo)點(diǎn)符號(hào)的話,帶著 } } } catch (BadHanyuPinyinOutputFormatCombination e) { System.out.println("字符不能轉(zhuǎn)成漢語(yǔ)拼音"); } return hanyupinyin; } public static String getPinyinString(String ChineseLanguage){ char[] cl_chars = ChineseLanguage.trim().toCharArray(); String hanyupinyin = ""; HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 輸出拼音全部大寫 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不帶聲調(diào) try { for (int i = 0; i < cl_chars.length; i++) { String str = String.valueOf(cl_chars[i]); if (str.matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,則將中文轉(zhuǎn)為漢語(yǔ)拼音,并取第一個(gè)字母 hanyupinyin += PinyinHelper.toHanyuPinyinStringArray( cl_chars[i], defaultFormat)[0]; } else if (str.matches("[0-9]+")) {// 如果字符是數(shù)字,取數(shù)字 hanyupinyin += cl_chars[i]; } else if (str.matches("[a-zA-Z]+")) {// 如果字符是字母,取字母 hanyupinyin += cl_chars[i]; } else {// 否則不轉(zhuǎn)換 } } } catch (BadHanyuPinyinOutputFormatCombination e) { System.out.println("字符不能轉(zhuǎn)成漢語(yǔ)拼音"); } return hanyupinyin; } /** * 取第一個(gè)漢字的第一個(gè)字符 * @Title: getFirstLetter * @Description: TODO * @return String * @throws */ public static String getFirstLetter(String ChineseLanguage){ char[] cl_chars = ChineseLanguage.trim().toCharArray(); String hanyupinyin = ""; HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);// 輸出拼音全部大寫 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不帶聲調(diào) try { String str = String.valueOf(cl_chars[0]); if (str.matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,則將中文轉(zhuǎn)為漢語(yǔ)拼音,并取第一個(gè)字母 hanyupinyin = PinyinHelper.toHanyuPinyinStringArray( cl_chars[0], defaultFormat)[0].substring(0, 1);; } else if (str.matches("[0-9]+")) {// 如果字符是數(shù)字,取數(shù)字 hanyupinyin += cl_chars[0]; } else if (str.matches("[a-zA-Z]+")) {// 如果字符是字母,取字母 hanyupinyin += cl_chars[0]; } else {// 否則不轉(zhuǎn)換 } } catch (BadHanyuPinyinOutputFormatCombination e) { System.out.println("字符不能轉(zhuǎn)成漢語(yǔ)拼音"); } return hanyupinyin; } public static void main(String[] args) { HanyuPinyinHelper hanyuPinyinHelper = new HanyuPinyinHelper() ; System.out.println(hanyuPinyinHelper.toHanyuPinyin("多發(fā)的發(fā)獨(dú)守空房阿道夫打發(fā)第三方")); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot注解之@EnableAutoConfiguration詳解
這篇文章主要介紹了Springboot注解之@EnableAutoConfiguration詳解,@EnableAutoConfiguration是一個(gè)加載Starter目錄包之外的需要Spring自動(dòng)生成bean對(duì)象,本文對(duì)其進(jìn)行總結(jié),需要的朋友可以參考下2023-08-08Spring中@DependsOn注解的使用代碼實(shí)例
這篇文章主要介紹了Spring中@DependsOn注解的使用代碼實(shí)例,Spring中@DependsOn,主要是使用在類和方法上, 作用是當(dāng)前對(duì)象要依賴另外一些對(duì)象,被依賴的對(duì)象會(huì)先注冊(cè)到Spring的IOC容器中,需要的朋友可以參考下2024-01-01springboot整合vue實(shí)現(xiàn)上傳下載文件
這篇文章主要為大家詳細(xì)介紹了springboot整合vue實(shí)現(xiàn)上傳下載文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11使用java采集京東商城區(qū)劃數(shù)據(jù)示例
這篇文章主要介紹了java采集京東的全國(guó)區(qū)劃數(shù)據(jù)示例,保存成json形式,如想轉(zhuǎn)換到數(shù)據(jù)庫(kù)只需反序列化為對(duì)象保存到數(shù)據(jù)庫(kù)即可2014-03-03java String類型對(duì)象轉(zhuǎn)換為自定義類型對(duì)象的實(shí)現(xiàn)
本文主要介紹了java String類型對(duì)象轉(zhuǎn)換為自定義類型對(duì)象的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Java中Socket實(shí)現(xiàn)數(shù)據(jù)通信的示例代碼
本文主要介紹了Java中Socket實(shí)現(xiàn)數(shù)據(jù)通信的示例代碼,Socket可以建立起客戶端和服務(wù)器之間的連接,實(shí)現(xiàn)數(shù)據(jù)的傳輸和交互,感興趣的可以了解一下2023-09-09Java并發(fā)系列之JUC中的Lock鎖與synchronized同步代碼塊問(wèn)題
這篇文章主要介紹了Java并發(fā)系列之JUC中的Lock鎖與synchronized同步代碼塊,簡(jiǎn)單介紹了lock鎖及鎖的底層知識(shí),結(jié)合案例給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04