Java提取汉字的拼音

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
    import java.io.PrintStream;  
    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;  
      
    public class Pinyin{  
      //拼音首字母  
      public static String converterToFirstSpell(String chines){  
        String pinyinName = "";  
        char[] nameChar = chines.toCharArray();  
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();  
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
        for (int i = 0; i < nameChar.length; ++i)  
          if (nameChar[i] > 128)  
            try {  
              pinyinName = pinyinName + net.sourceforge.pinyin4j.PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0].charAt(0);  
            } catch (BadHanyuPinyinOutputFormatCombination e) {  
              e.printStackTrace();  
            }  
          else  
            pinyinName = pinyinName + nameChar[i];  
      
      
        return pinyinName;  
      }  
      //全部转为拼音  
      public static String converterToSpell(String chines){  
        String pinyinName = "";  
        char[] nameChar = chines.toCharArray();  
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();  
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
        for (int i = 0; i < nameChar.length; ++i)  
          if (nameChar[i] > 128)  
            try {  
              pinyinName = pinyinName + net.sourceforge.pinyin4j.PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0];  
            } catch (BadHanyuPinyinOutputFormatCombination e) {  
              e.printStackTrace();  
            }  
          else  
            pinyinName = pinyinName + nameChar[i];  
      
      
        return pinyinName;  
      }  
        
      public static void main(String[] args) {  
        System.out.println(converterToSpell("我是你大哥!!"));  
      }  
    }  

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:java使用IMAP连接Gmail并解析邮件

下一篇:使用jetty实现小型的Servlet服务器