Java獲取漢字對應(yīng)的拼音(全拼或首字母)
Java 根據(jù)漢語字符串獲得對應(yīng)的拼音字符串或者拼音首字母字符串等操作,需要添加jar包:
代碼實(shí)現(xiàn):
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
/***
* 漢字工具類
* @author csharper
* @since 2014.12.26
*
*/
public class ChineseCharacterUtil {
/***
* 將漢字轉(zhuǎn)成拼音(取首字母或全拼)
* @param hanzi
* @param full 是否全拼
* @return
*/
public static String convertHanzi2Pinyin(String hanzi,boolean full)
{
/***
* ^[\u2E80-\u9FFF]+$ 匹配所有東亞區(qū)的語言
* ^[\u4E00-\u9FFF]+$ 匹配簡體和繁體
* ^[\u4E00-\u9FA5]+$ 匹配簡體
*/
String regExp="^[\u4E00-\u9FFF]+$";
StringBuffer sb=new StringBuffer();
if(hanzi==null||"".equals(hanzi.trim()))
{
return "";
}
String pinyin="";
for(int i=0;i<hanzi.length();i++)
{
char unit=hanzi.charAt(i);
if(match(String.valueOf(unit),regExp))//是漢字,則轉(zhuǎn)拼音
{
pinyin=convertSingleHanzi2Pinyin(unit);
if(full)
{
sb.append(pinyin);
}
else
{
sb.append(pinyin.charAt(0));
}
}
else
{
sb.append(unit);
}
}
return sb.toString();
}
/***
* 將單個(gè)漢字轉(zhuǎn)成拼音
* @param hanzi
* @return
*/
private static String convertSingleHanzi2Pinyin(char hanzi)
{
HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
String[] res;
StringBuffer sb=new StringBuffer();
try {
res = PinyinHelper.toHanyuPinyinStringArray(hanzi,outputFormat);
sb.append(res[0]);//對于多音字,只用第一個(gè)拼音
} catch (Exception e) {
e.printStackTrace();
return "";
}
return sb.toString();
}
/***
* @param str 源字符串
* @param regex 正則表達(dá)式
* @return 是否匹配
*/
public static boolean match(String str,String regex)
{
Pattern pattern=Pattern.compile(regex);
Matcher matcher=pattern.matcher(str);
return matcher.find();
}
public static void main(String[] args) {
System.out.println(convertHanzi2Pinyin("我是中國人123abc",true));
}
}
運(yùn)行結(jié)果:
(1)全拼:
woshizhongguoren123abc
(2)首字母:
wszgr123abc
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
JavaMail實(shí)現(xiàn)簡單郵件發(fā)送
這篇文章主要為大家詳細(xì)介紹了JavaMail實(shí)現(xiàn)簡單郵件發(fā)送,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
Javabean轉(zhuǎn)換成json字符并首字母大寫代碼實(shí)例
這篇文章主要介紹了javabean轉(zhuǎn)成json字符并首字母大寫代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
Aop動態(tài)代理和cglib實(shí)現(xiàn)代碼詳解
這篇文章主要介紹了Aop動態(tài)代理和cglib實(shí)現(xiàn)代碼詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
使用ScheduledThreadPoolExecutor踩過最痛的坑
這篇文章主要介紹了使用ScheduledThreadPoolExecutor踩過最痛的坑及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
mybatis 中 foreach collection的用法小結(jié)(三種)
這篇文章主要介紹了mybatis 中 foreach collection的用法小結(jié)(三種),需要的朋友可以參考下2017-10-10
mybatis-plus實(shí)體類主鍵策略有3種(小結(jié))
這篇文章主要介紹了mybatis-plus實(shí)體類主鍵策略有3種(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Plugin ‘org.springframework.boot:spring-boot-maven-plug
這篇文章給大家介紹了Plugin ‘org.springframework.boot:spring-boot-maven-plugin:‘ not found的解決方案,親測可用,文中給出了兩種解決方法,需要的朋友可以參考下2024-01-01

