a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 111|回复: 1

[基础知识] JAVA基础:java代码如何将汉字转换成拼音(1)

[复制链接]
发表于 2012-8-4 12:37:27 | 显示全部楼层 |阅读模式
 package com.sucre;   //所需导入的jar包在下侧下载0 O% y2 ?* R# f* c/ |
  import net.sourceforge.pinyin4j.PinyinHelper;) a# [0 \% A) E4 d8 @  t" y
  import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;7 K+ S% I1 Q/ \& |" x
  import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
  X0 J5 f. ]+ ]: J; j; n  import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
2 ~/ E: u; h4 N1 T+ A  import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
  r6 H& F4 @8 p# N- z# F: [+ a  import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
) f4 T4 `5 }& ~" w5 q  public class ChineseToEnglish1 s/ `, y' M# E
  {
6 ^) W- k; w/ \  u( T  //------------------------将汉字转换为全拼-------------------------------" Y" l6 r! c5 k7 I0 o, K7 T/ z
  public static String getPingYin(String src){
! g/ N; t2 V+ r/ u; l1 g  char[] t1 = null;0 C$ Y! w( ~" a& s
  t1=src.toCharArray();
& Z+ u% f$ @: q9 L- l" h% y, b  String[] t2 = new String[t1.length];
, b* c# f, J# Q2 a, r! A, M  HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();8 s+ [/ S& ]! Z
  t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);' e7 L# E0 N. {2 |3 V) g% [
  t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);, ^  R# q1 y1 n8 I* f
  t3.setVCharType(HanyuPinyinVCharType.WITH_V);7 K# F, T" x! F" q' S" F  u$ W0 i2 i
  String t4="";. f' B$ i5 ~- c( {9 i7 n% D
  int t0=t1.length;
) _1 ^- Y6 o2 `$ M% ~  try {& Q) @& M% `% }7 m( N# j7 N5 W
+ u9 T+ L- K2 e. W* Z# o& [
  for (int i=0;i
回复

使用道具 举报

 楼主| 发表于 2012-8-4 12:37:28 | 显示全部楼层

JAVA基础:java代码如何将汉字转换成拼音(1)

</p>  //判断是否为汉字字符
9 `8 F; C2 k- I' U! q: D* h8 ?  if(java.lang.Character.toString(t1).matches("[\\u4E00-\\u9FA5]+")){; [, V/ y3 V, O- o% `* `
  t2 = PinyinHelper.toHanyuPinyinStringArray(t1, t3);
, N8 t, d0 T  Z  t4+=t2[0];
: W% G* [" q7 t' E" n  }else{0 v1 c& p$ b* z9 _0 s/ b. K' z
  t4+=java.lang.Character.toString(t1);3 q; Z9 Z& j5 |4 y. L& A
  }; _5 p5 ^  T: n  _2 R
  }
- r; u7 L& ?, F  //System.out.println(t4);% D1 T4 E) R4 |( \% V0 L  T! F
  return t4;
2 j, _* ^% o& m2 U7 {  }catch (BadHanyuPinyinOutputFormatCombination e1){
4 d, \2 R& e- U8 T& o  e1.printStackTrace();
4 I$ L: {5 [; j1 D. s6 u4 D  }
2 E7 g6 v. a4 F0 Q# c  return t4;5 B. V6 q/ k) T" A5 I
  }
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Woexam.Com ( 湘ICP备18023104号 )

GMT+8, 2024-5-16 09:13 , Processed in 0.253265 second(s), 23 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表