将汉字转换为拼音

一:演示:
    如在控制台输入:北京欢迎你

    打印出来的拼音: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 {
                	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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值