pinyin4j:获取中文串拼音或拼音首字母

简介: pinyin4j:获取中文串拼音或拼音首字母
package pinyin;
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.exception.BadHanyuPinyinOutputFormatCombination;
/**
 * @Title:
 * @ClassName: pinyin.PinYinUtil.java
 * @Description: pinyin4j的Maven坐标
 *
 *   <dependency>
 *   <groupId>com.belerweb</groupId>
 *   <artifactId>pinyin4j</artifactId>
 *   <version>2.5.0</version>
 *   </dependency>
 *
 * @Copyright 2016-2017  - Powered By 研发中心
 * @author: FLY
 * @date:  2017-11-13 17:09
 * @version V1.0
 */
public class PinYinUtil {
    /**
     * @Title: 获取中文串拼音首字母,英文字符不变
     * @methodName:  getFirstSpell
     * @param chinese 汉字串
     * @return java.lang.String 中文拼音首字母
     * @Description:
     *
     * @author: FLY
     * @date:  2017-11-13 17:13
     */
    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(); 
    } 
    /**
     * @Title:  获取中文串拼音,英文字符不变
     * @methodName:  getFullSpell
     * @param chinese 中文字符串
     * @return java.lang.String  中文拼音
     * @Description:
     *
     * @author: FLY
     * @date:  2017-11-13 17:15
     */
    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) {
        System.out.println(getFirstSpell("你好"));// nh
        System.out.println(getFullSpell("你好"));//nihao
    }
}
目录
相关文章
|
6月前
中文转拼音
中文转拼音
71 0
带声调的拼音字符比较特殊
带声调的拼音字符比较特殊
99 0
推荐一个好用的汉字转拼音的插件
前阶段做项目时里面有一个小功能,就是输入名字之后,将其转换成拼音,然后填入另一个需要输入的文本框中,在调查一番后,发现了一个比较符合自己需求的一款插件,
361 0
C# 获取汉字拼音首字母
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来。 十年河东十年河西,莫欺少年穷 学无止境,精益求精   本节探讨C#获取汉字拼音首字母的方法: 代码类东西,直接上代码: /// /// 在指定的字符串列表CnStr中检...
3204 0