Pinyin4j入门教程
http://blog.csdn.net/hfhwfw/article/details/6030816
1 2 3 4 5 6 |
Xml代码 <dependency> <groupId>com.belerweb</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.0</version> </dependency> |
获得拼音:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
Java代码 package com.pandy.test; /** * 项目名称: wp_idea_linux * 功能说明: * 创建者: Pandy, * 邮箱: panyongzheng@163.com, 1453261799@qq.com * 版权: * 官网: * 创建日期: 15-8-18. * 创建时间: 上午8:50. * 修改历史: * ----------------------------------------------- */ 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 PinYinUtil { public static String getPinYin(String inputString) { HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); format.setCaseType(HanyuPinyinCaseType.LOWERCASE); format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK); format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE); char[] input = inputString.trim().toCharArray(); StringBuffer output = new StringBuffer(""); try { for (int i = 0; i < input.length; i++) { if (Character.toString(input[i]).matches("[\u4E00-\u9FA5]+")) { String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format); output.append(temp[0]); output.append(" "); } else output.append(Character.toString(input[i])); } } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } return output.toString(); } public static void main(String[] args) { String chs = "单词,子弹,弹琴,我是中国人! I'm Chinese!"; System.out.println(chs); System.out.println(getPinYin(chs)); } } |
单词,子弹,弹琴,我是中国人! I’m Chinese!
dān cí ,zi dàn ,dàn qín ,wŏ shì zhōng guó rén ! I’m Chinese!
用pinyin4j获取汉语拼音并首字母大写
http://www.xuehuile.com/blog/c2481dcface541a186ffee657f47f33c.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
Java代码 package com.javaeye.i2534; 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 SpellKit { /** * 获取字符串内的所有汉字的汉语拼音并大写每个字的首字母 * * @param chinese * @return */ public static String spell(String chinese) { if (chinese == null) { return null; } HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); format.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 小写 format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不标声调 format.setVCharType(HanyuPinyinVCharType.WITH_V);// u:的声母替换为v try { StringBuilder sb = new StringBuilder(); for (int i = 0; i < chinese.length(); i++) { String[] array = PinyinHelper.toHanyuPinyinStringArray(chinese .charAt(i), format); if (array == null || array.length == 0) { continue; } String s = array[0];// 不管多音字,只取第一个 char c = s.charAt(0);// 大写第一个字母 String pinyin = String.valueOf(c).toUpperCase().concat(s .substring(1)); sb.append(pinyin); } return sb.toString(); } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } return null; } /** * @param args */ public static void main(String[] args) { System.out.println(SpellKit.spell("刘宝瑞")); } } |