热转印www.yxheatpress.com

公司网站模板http://qiyemoban.software8.co/

常用链接

统计

友情链接

最新评论

【你不知道的Java】-汉字转换拼音

一:

String cnStr = "钓鱼岛是中国的";

diao yu dao shi zhong guo de 

二:code

  1. public static String getPinYin(String src) {  
  2.         char[] t1 = null;  
  3.         t1 = src.toCharArray();  
  4.         // System.out.println(t1.length);  
  5.         String[] t2 = new String[t1.length];  
  6.         // System.out.println(t2.length);  
  7.         // 设置汉字拼音输出的格式  
  8.         HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();  
  9.         t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
  10.         t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
  11.         t3.setVCharType(HanyuPinyinVCharType.WITH_V);  
  12.         String t4 = "";  
  13.         int t0 = t1.length;  
  14.         try {  
  15.             for (int i = 0; i < t0; i++) {  
  16.                 // 判断能否为汉字字符  
  17.                 // System.out.println(t1[i]);  
  18.                 if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {  
  19.                     t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);// 将汉字的几种全拼都存到t2数组中  
  20.                     t4 += t2[0]+" ";// 取出该汉字全拼的第一种读音并连接到字符串t4后  
  21.                 } else {  
  22.                     // 如果不是汉字字符,间接取出字符并连接到字符串t4后  
  23.                     t4 += Character.toString(t1[i]);  
  24.                 }  
  25.             }  
  26.         } catch (BadHanyuPinyinOutputFormatCombination e) {  
  27.             e.printStackTrace();  
  28.         }  
  29.         return t4;  
  30.     }  

posted on 2012-10-17 09:50 不听话的 阅读(1134) 评论(2)  编辑 收藏 引用

评论

# re: 【你不知道的Java】-汉字转换拼音 2012-10-17 17:49 王世雄

有意思呵呵。不过钓鱼岛是日本的。  回复  更多评论   

# re: 【你不知道的Java】-汉字转换拼音[未登录] 2013-05-04 14:59 albert

不怎么样,就是简单地词典啊?  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理