您的位置:首页 > 其它

将汉字转换为拼音

2017-01-14 23:01 393 查看
一:演示:

    如在控制台输入:北京欢迎你
    打印出来的拼音: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: