中文转拼音
来源:互联网 发布:软件技术支持服务 编辑:程序博客网 时间:2024/05/05 04:50
/* * 1.创建java project * 2.将项目转变为Maven项目 * 3.pom.xml中导入两个架包 <dependency> <groupId>com.belerweb</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.1</version> </dependency> <dependency> <groupId>com.belerweb</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.1</version> </dependency>*4.写一个main方法*/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 ChineseToPinyin { public static void main(String[] args) { String str = "蓝礼巍"; String resultShort = getCnShort(str); System.out.println(str + "的拼音首字母是 : " + resultShort); String result=getCnAll(str); System.out.println(str + "的拼音是 : " + result); } static HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); static { format.setCaseType(HanyuPinyinCaseType.LOWERCASE); format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); format.setVCharType(HanyuPinyinVCharType.WITH_V); } // 取首字母 public static String getCnShort(String str) { String cnShort = ""; int length = str.length(); for (int i = 0; i < length; i++) { char c = str.charAt(i); try { String[] pinyin = PinyinHelper.toHanyuPinyinStringArray(c, format); if (pinyin.length > 0) { cnShort = cnShort + pinyin[0].charAt(0); } } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } } return cnShort; } //取拼音 public static String getCnAll(String str){ String cnAll = ""; int length=str.length(); for(int i=0;i<length;i++){ char c = str.charAt(i); try { String[] pinyin = PinyinHelper.toHanyuPinyinStringArray(c, format); cnAll = cnAll + pinyin[0]; } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } } return cnAll; }}
0 0
- 中文转拼音首字母
- SQL中文转拼音
- C#中文转拼音
- 中文转拼音
- java中文转拼音
- SQL 中文转拼音
- Flex中文转拼音
- 中文转拼音(php)
- 中文转拼音JS
- asp中文转拼音
- 中文 转 拼音
- java 中文转拼音
- 中文转拼音
- Flex中文转拼音
- java中文转拼音
- 中文转拼音
- JAVA 中文转拼音
- 中文转拼音
- 51nod 1043 幸运号码【dp+滚动数组】
- 词法分析程序设计
- oracle中监控表中的索引是否使用
- 编译简单优先分析法
- 二叉搜索树的第k个结点
- 中文转拼音
- 二进制求和,lintcode
- window10 安装完后 几个小优化
- 通过Java实现Linux系统下grep指令的功能
- sample_allocator 参考手册
- Eclipse快捷键大全
- ACdream1082-闪电战
- 引用外部js文件常见问题!
- Android Data Binding(结合RecyclerView动态绑定数据)