将汉字转换为拼音
2017-01-14 23:01
393 查看
一:演示:
如在控制台输入:北京欢迎你
打印出来的拼音:bei jing huan ying ni
二:导入要依赖的jar:
汉字转换拼音jar下载
三:代码编写
四:输出结果为:
3
#
3214zhonghuarenmingongheguo
33323134e4b8ade58d8ee4babae6b091e585b1e5928ce59bbd
如在控制台输入:北京欢迎你
打印出来的拼音:bei jing huan ying ni
二:导入要依赖的jar:
汉字转换拼音jar下载
<!-- maven仓库中pom.xml的配置 --> <dependency> <groupId>net.sourceforge.pinyin4j</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.0</version> </dependency>
三:代码编写
package com.common.util; 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; /** * 拼音工具类 * * @author yubingrong 2017年2月17日 * */ public class PinYinUtils { /** * 将汉字转换为全拼 * * @param word * @return String * @throws BadHanyuPinyinOutputFormatCombination */ public static String getPinYin(String word) throws BadHanyuPinyinOutputFormatCombination { // 设置汉字拼音输出的格式 HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); format.setCaseType(HanyuPinyinCaseType.LOWERCASE); format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); format.setVCharType(HanyuPinyinVCharType.WITH_V); StringBuffer sb = new StringBuffer(); for (int i = 0; i < word.length(); i++) { char c = word.charAt(i); // 判断是否为汉字字符 if (Character.toString(c).matches("[\\u4E00-\\u9FA5]+")) { String[] pyStrArray = PinyinHelper.toHanyuPinyinStringArray(c, format);// 将汉字的几种全拼都存到t2数组中 // 取出该汉字全拼的第一种读音 sb.append(pyStrArray[0]); } else { // 如果不是汉字字符,直接取出字符 sb.append(Character.toString(c)); } } return sb.toString(); } /** * 提取每个汉字的首字母 * * @param word * @return String * @throws BadHanyuPinyinOutputFormatCombination */ public static String getPinYinHeadChar(String word) throws BadHanyuPinyinOutputFormatCombination { // 设置汉字拼音输出的格式 HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); format.setCaseType(HanyuPinyinCaseType.UPPERCASE); format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); format.setVCharType(HanyuPinyinVCharType.WITH_V); StringBuffer sb = new StringBuffer(); for (int i = 0; i < word.length(); i++) { char c = word.charAt(i); // 提取汉字的首字母 if (Character.toString(c).matches("[\\u4E00-\\u9FA5]+")) { String[] pyStrArray = PinyinHelper.toHanyuPinyinStringArray(c,format); // 取出该汉字全拼的第一种读音 sb.append(pyStrArray[0].charAt(0)); }else{ if (Character.toString(c).matches("[A-Za-z]")) { sb.append(Character.toString(c).toUpperCase()); } else { 4000 sb.append("#"); } } } return sb.toString(); } /** * 将字符串转换成ASCII码 * * @param cnStr * @return String */ public static String getCnASCII(String cnStr) { StringBuffer strBuf = new StringBuffer(); // 将字符串转换成字节序列 byte[] bGBK = cnStr.getBytes(); for (int i = 0; i < bGBK.length; i++) { // 将每个字符转换成ASCII码 strBuf.append(Integer.toHexString(bGBK[i] & 0xff)); } return strBuf.toString(); } public static void main(String[] args) throws BadHanyuPinyinOutputFormatCombination { String cnStr = "3214中华人民共和国"; char c = cnStr.charAt(0); System.out.println(getPinYin(Character.toString(c))); System.out.println(getPinYinHeadChar(Character.toString(c))); System.out.println(getPinYin(cnStr)); System.out.println(getCnASCII(cnStr)); } }
四:输出结果为:
3
#
3214zhonghuarenmingongheguo
33323134e4b8ade58d8ee4babae6b091e585b1e5928ce59bbd
相关文章推荐
- java中汉字自动转换成拼音
- 汉字转换成拼音的代码(asp版)
- SQL:汉字转换拼音或拼音首字母
- (旧文重贴)花了一个小时时间,给项目组作了一个汉字到拼音的转换
- C#汉字转拼音(npinyin)将中文转换成拼音全文或首字母
- Java 汉字转换为中文拼音的研究一:读取.db文件
- java 汉字转换成拼音
- 汉字转换成拼音的代码(JS版)
- python中将汉字转换为拼音的库xpinyin
- C# 将汉字转换成拼音
- C#汉字转换拼音技术详解(高性能)
- C#汉字转换拼音技术详解
- Javascript输入汉字自动转换为首字母的拼音码
- Java下将汉字转换为拼音的包pinyin4j
- Delphi数据库的拼音检索(查询)方案-汉字转换成拼音
- js汉字转换拼音
- 转换实现转换汉字拼音首字母
- Java下将汉字转换为拼音的包pinyin4j
- SQL:汉字转换拼音或拼音首字母
- Java 汉字转换为拼音字符串