欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java 獲取中文拼音首字母及全拼的實(shí)踐

 更新時(shí)間:2022年08月10日 10:09:51   作者:Coder-CT  
本文主要介紹了java 獲取中文拼音首字母及全拼的實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

使用Hutool工具類 官網(wǎng)鏈接

以下為Hutool支持的拼音庫的pom坐標(biāo),你可以選擇任意一個(gè)引入項(xiàng)目中,如果引入多個(gè),Hutool會(huì)按照以上順序選擇第一個(gè)使用。

<dependency>
    <groupId>io.github.biezhi</groupId>
    <artifactId>TinyPinyin</artifactId>
    <version>2.0.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>com.belerweb</groupId>
    <artifactId>pinyin4j</artifactId>
    <version>2.5.1</version>
</dependency>
<dependency>
    <groupId>com.github.stuxuhai</groupId>
    <artifactId>jpinyin</artifactId>
    <version>1.1.8</version>
</dependency>

使用鏈接

查看Hutool最新版本

           <!--詞庫-->
        <dependency>
            <groupId>io.github.biezhi</groupId>
            <artifactId>TinyPinyin</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>
           <!--Hutool工具類-->
        <dependency>
           <groupId>cn.hutool</groupId>
           <artifactId>hutool-all</artifactId>
           <version>5.8.4</version>
        </dependency>
import cn.hutool.extra.pinyin.PinyinUtil;

public class Test {
? ? public static void main(String[] args) {
? ? ? ? // 獲取全部漢字首字母,第二個(gè)參數(shù)為分隔符
? ? ? ? String str1 = PinyinUtil.getFirstLetter("測試","-"); //c-s
? ? ? ? // 返回全部拼音 默認(rèn)分隔符為空格,可以添加第二個(gè)參數(shù)分隔符
? ? ? ? String str2 = PinyinUtil.getPinyin("測試"); // ce shi
? ? ? ? String str3 = PinyinUtil.getPinyin("測試","-");// ce-shi
? ? }
}

判斷字符串是否為中文

 //判斷是否為中文
    private static Boolean isChinese(String str) {
        if (str.trim().matches("[\u4E00-\u9FA5]+")) {
            return true;
        } else return false;
    }

PS:其他實(shí)現(xiàn)方法

第一種:

直接上代碼(有個(gè)別中文無法識別):

 import java.io.UnsupportedEncodingException;
/**
 * 
 * @author yuki_ho
 *
 */
public class ChineseCharToEnUtil {
      private final static int[] li_SecPosValue = { 1601, 1637, 1833, 2078, 2274,  
                2302, 2433, 2594, 2787, 3106, 3212, 3472, 3635, 3722, 3730, 3858,  
                4027, 4086, 4390, 4558, 4684, 4925, 5249, 5590 };  
        private final static String[] lc_FirstLetter = { "a", "b", "c", "d", "e",  
                "f", "g", "h", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",  
                "t", "w", "x", "y", "z" };  
      
        /** 
         * 取得給定漢字串的首字母串,即聲母串 
         * @param str 給定漢字串 
         * @return 聲母串 
         */  
        public String getAllFirstLetter(String str) {  
            if (str == null || str.trim().length() == 0) {  
                return "";  
            }  
      
            String _str = "";  
            for (int i = 0; i < str.length(); i++) {  
                _str = _str + this.getFirstLetter(str.substring(i, i + 1));  
            }  
      
            return _str;  
        }  
      
        /** 
         * 取得給定漢字的首字母,即聲母 
         * @param chinese 給定的漢字 
         * @return 給定漢字的聲母 
         */  
        public String getFirstLetter(String chinese) {  
            if (chinese == null || chinese.trim().length() == 0) {  
                return "";  
            }  
            chinese = this.conversionStr(chinese, "GB2312", "ISO8859-1");  
      
            if (chinese.length() > 1) // 判斷是不是漢字  
            {  
                int li_SectorCode = (int) chinese.charAt(0); // 漢字區(qū)碼  
                int li_PositionCode = (int) chinese.charAt(1); // 漢字位碼  
                li_SectorCode = li_SectorCode - 160;  
                li_PositionCode = li_PositionCode - 160;  
                int li_SecPosCode = li_SectorCode * 100 + li_PositionCode; // 漢字區(qū)位碼  
                if (li_SecPosCode > 1600 && li_SecPosCode < 5590) {  
                    for (int i = 0; i < 23; i++) {  
                        if (li_SecPosCode >= li_SecPosValue[i]  
                                && li_SecPosCode < li_SecPosValue[i + 1]) {  
                            chinese = lc_FirstLetter[i];  
                            break;  
                        }  
                    }  
                } else // 非漢字字符,如圖形符號或ASCII碼  
                {  
                    chinese = this.conversionStr(chinese, "ISO8859-1", "GB2312");  
                    chinese = chinese.substring(0, 1);  
                }  
            }  
      
            return chinese;  
        }  
      
        /** 
         * 字符串編碼轉(zhuǎn)換 
         * @param str 要轉(zhuǎn)換編碼的字符串 
         * @param charsetName 原來的編碼 
         * @param toCharsetName 轉(zhuǎn)換后的編碼 
         * @return 經(jīng)過編碼轉(zhuǎn)換后的字符串 
         */  
        private String conversionStr(String str, String charsetName,String toCharsetName) {  
            try {  
                str = new String(str.getBytes(charsetName), toCharsetName);  
            } catch (UnsupportedEncodingException ex) {  
                System.out.println("字符串編碼轉(zhuǎn)換異常:" + ex.getMessage());  
            }  
            return str;  
        }  
      
        public static void main(String[] args) {  
            ChineseCharToEnUtil cte = new ChineseCharToEnUtil();  
            System.out.println("獲取拼音首字母:"+ cte.getAllFirstLetter("廣州"));  
        }  
      
}

第二種:

所需包:net.sourceforge.pinyin4j

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;
/**
 * 
 * @author yuki_ho
 * @time   2017-07-25
 */
public class ChineseCharToEnUtil {
 
 
   /**
     * 將字符串中的中文轉(zhuǎn)化為拼音,其他字符不變
     * 
     * @param inputString
     * @return
     */
    public static String getPingYin(String inputString) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
 
        char[] input = inputString.trim().toCharArray();
        String output = "";
 
        try {
            for (int i = 0; i < input.length; i++) {
                if (java.lang.Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")) {
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);
                    output += temp[0];
                } else
                    output += java.lang.Character.toString(input[i]);
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }
        return output;
    }
    /**  
     * 獲取漢字串拼音首字母,英文字符不變  
     * @param chinese 漢字串  
     * @return 漢語拼音首字母  
     */  
    public static String getFirstSpell(String chinese) {   
            StringBuffer pybf = new StringBuffer();   
            char[] arr = chinese.toCharArray();   
            HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();   
            defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);   
            defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);   
            for (int i = 0; i < arr.length; i++) {   
                    if (arr[i] > 128) {   
                            try {   
                                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);   
                                    if (temp != null) {   
                                            pybf.append(temp[0].charAt(0));   
                                    }   
                            } catch (BadHanyuPinyinOutputFormatCombination e) {   
                                    e.printStackTrace();   
                            }   
                    } else {   
                            pybf.append(arr[i]);   
                    }   
            }   
            return pybf.toString().replaceAll("\\W", "").trim();   
    }   
    /**  
     * 獲取漢字串拼音,英文字符不變  
     * @param chinese 漢字串  
     * @return 漢語拼音  
     */  
    public static String getFullSpell(String chinese) {   
            StringBuffer pybf = new StringBuffer();   
            char[] arr = chinese.toCharArray();   
            HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();   
            defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);   
            defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);   
            for (int i = 0; i < arr.length; i++) {   
                    if (arr[i] > 128) {   
                            try {   
                                    pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]);   
                            } catch (BadHanyuPinyinOutputFormatCombination e) {   
                                    e.printStackTrace();   
                            }   
                    } else {   
                            pybf.append(arr[i]);   
                    }   
            }   
            return pybf.toString();   
    }  
    
    public static void main(String[] args)
    {
        String cnStr = "謳萘";
        System.out.println("謳萘-->" + getPingYin(cnStr));
        String s = getFirstSpell("謳萘");
        System.out.println("謳萘-->" + s);
        StringBuffer sb = new StringBuffer(s);
        if (sb.length() > 1)
        {
            String ss = sb.delete(1, sb.length()).toString();
            System.out.println("謳萘-->"
                    + Character.toUpperCase(ss.toCharArray()[0]) + "");
        }
    }
}

到此這篇關(guān)于java 獲取中文拼音首字母及全拼的實(shí)踐的文章就介紹到這了,更多相關(guān)java 獲取中文拼音首字母及全拼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • spring-boot-maven-plugin引入出現(xiàn)爆紅(已解決)

    spring-boot-maven-plugin引入出現(xiàn)爆紅(已解決)

    這篇文章主要介紹了spring-boot-maven-plugin引入出現(xiàn)爆紅(已解決),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • 關(guān)于@PropertySource配置的用法解析

    關(guān)于@PropertySource配置的用法解析

    這篇文章主要介紹了關(guān)于@PropertySource配置的用法解析,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 在IDEA中使用debug工具去運(yùn)行java程序的實(shí)現(xiàn)步驟

    在IDEA中使用debug工具去運(yùn)行java程序的實(shí)現(xiàn)步驟

    調(diào)試工具(debug工具)是一種用于幫助程序員識別和修復(fù)程序中的錯(cuò)誤的工具,它們提供了一系列的功能,幫助程序員在代碼執(zhí)行的過程中跟蹤和檢測問題,本文將給大家介紹使用debug工具去運(yùn)行java程序的實(shí)現(xiàn)步驟,需要的朋友可以參考下
    2024-04-04
  • Java中的OpenTracing使用實(shí)例

    Java中的OpenTracing使用實(shí)例

    這篇文章主要介紹了Java中的OpenTracing使用實(shí)例,主要的OpenTracing API將所有主要組件聲明為接口以及輔助類,例如Tracer,Span,SpanContext,Scope,ScopeManager,Format(用映射定義通用的SpanContext注入和提取格式),需要的朋友可以參考下
    2024-01-01
  • Java中的SpringAOP、代理模式、常用AspectJ注解詳解

    Java中的SpringAOP、代理模式、常用AspectJ注解詳解

    這篇文章主要介紹了Java中的SpringAOP、代理模式、常用AspectJ注解詳解,Spring提供了面向切面編程的豐富支持,允許通過分離應(yīng)用的業(yè)務(wù)邏輯與系統(tǒng)級服務(wù),例如審計(jì)和事務(wù)管理進(jìn)行內(nèi)聚性的開發(fā),需要的朋友可以參考下
    2023-09-09
  • logback 自定義Pattern模板教程

    logback 自定義Pattern模板教程

    這篇文章主要介紹了logback 自定義Pattern模板教程,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(21)

    Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(21)

    下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你
    2021-07-07
  • SpringCloud入門實(shí)驗(yàn)環(huán)境搭建

    SpringCloud入門實(shí)驗(yàn)環(huán)境搭建

    這篇文章主要介紹了SpringCloud入門實(shí)驗(yàn)環(huán)境搭建的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用SpringCloud,感興趣的朋友可以了解下
    2021-04-04
  • Java中初始化List集合的八種方式匯總

    Java中初始化List集合的八種方式匯總

    List?是?Java?開發(fā)中經(jīng)常會(huì)使用的集合,下面這篇文章主要給大家介紹了關(guān)于Java中初始化List集合的八種方式,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • javap命令的使用技巧

    javap命令的使用技巧

    本篇文章給大家分享了關(guān)于JAVA中關(guān)于javap命令的使用技巧以及相關(guān)代碼分享,有需要的朋友參考學(xué)習(xí)下。
    2018-05-05

最新評論