最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java 獲取中文拼音首字母及全拼的實踐

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

使用Hutool工具類 官網鏈接

以下為Hutool支持的拼音庫的pom坐標,你可以選擇任意一個引入項目中,如果引入多個,Hutool會按照以上順序選擇第一個使用。

<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) {
? ? ? ? // 獲取全部漢字首字母,第二個參數為分隔符
? ? ? ? String str1 = PinyinUtil.getFirstLetter("測試","-"); //c-s
? ? ? ? // 返回全部拼音 默認分隔符為空格,可以添加第二個參數分隔符
? ? ? ? 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:其他實現(xiàn)方法

第一種:

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

 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;  
        }  
      
        /** 
         * 字符串編碼轉換 
         * @param str 要轉換編碼的字符串 
         * @param charsetName 原來的編碼 
         * @param toCharsetName 轉換后的編碼 
         * @return 經過編碼轉換后的字符串 
         */  
        private String conversionStr(String str, String charsetName,String toCharsetName) {  
            try {  
                str = new String(str.getBytes(charsetName), toCharsetName);  
            } catch (UnsupportedEncodingException ex) {  
                System.out.println("字符串編碼轉換異常:" + 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 {
 
 
   /**
     * 將字符串中的中文轉化為拼音,其他字符不變
     * 
     * @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]) + "");
        }
    }
}

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

相關文章

最新評論

普格县| 时尚| 嘉黎县| 宣恩县| 孟州市| 无极县| 河津市| 天台县| 锦州市| 西吉县| 耿马| 错那县| 且末县| 西城区| 朝阳县| 同仁县| 海阳市| 常山县| 阿拉善左旗| 句容市| 彰化市| 天全县| 龙州县| 石门县| 佳木斯市| 龙门县| 凤凰县| 林口县| 雷山县| 通江县| 沿河| 新乡县| 陇西县| 郸城县| 星子县| 三河市| 黄平县| 北京市| 承德县| 永兴县| 腾冲县|