java代碼將漢字轉換成拼音

來源:互聯網
上載者:User

package com.sucre;
 
  //所需匯入的jar包在下側下載
 
  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;
 
  public class ChineseToEnglish
 
  {
 
  //------------------------將漢字轉換為全拼-------------------------------
 
  public static String getPingYin(String src){
 
  char[] t1 = null;
 
  t1=src.toCharArray();
 
  String[] t2 = new String[t1.length];
 
  HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
 
  t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
 
  t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
 
  t3.setVCharType(HanyuPinyinVCharType.WITH_V);
 
  String t4="";
 
  int t0=t1.length;
 
  try {
 
  for (int i=0;i<t0;i++){
 
  //判斷是否為漢字字元
 
  if(java.lang.Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")){
 
  t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
 
  t4+=t2[0];
 
  }else{
 
  t4+=java.lang.Character.toString(t1[i]);
 
  }
 
  }
 
  //System.out.println(t4);
 
  return t4;
 
  }catch (BadHanyuPinyinOutputFormatCombination e1){
 
  e1.printStackTrace();
 
  }
 
  return t4;
 
  }

本文來自:http://www.yuloo.com/news/1108/688672.html

jar包:http://pinyin4j.sourceforge.net/

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.