将汉字转换为拼音
来源:互联网 发布:数据归一化方法及应用 编辑:程序博客网 时间:2024/05/02 18:21
一:演示:
如在控制台输入:北京欢迎你
如在控制台输入:北京欢迎你
打印出来的拼音: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#3214zhonghuarenmingongheguo33323134e4b8ade58d8ee4babae6b091e585b1e5928ce59bbd
0 0
- 将汉字转换为拼音
- 将汉字转换为拼音
- 将汉字转换为拼音
- 将汉字转换为拼音
- C#将汉字转换为拼音[转]
- JS将汉字转换为拼音
- C#将汉字转换为拼音首字母
- 使用CFStringTransform将汉字转换为拼音
- 使用CFStringTransform将汉字转换为拼音
- Java将汉字转换为拼音
- 使用CFStringTransform将汉字转换为拼音
- 使用CFStringTransform 将汉字转换为拼音
- 使用CFStringTransform 将汉字转换为拼音
- 使用CFStringTransform将汉字转换为拼音
- 如何快速将汉字转换为拼音
- 使用CFStringTransform将汉字转换为拼音
- iOS 将汉字转换为拼音
- iOS将汉字转换为拼音
- MyBatis传入多个参数的问题
- static1----Java基础041
- 带通信号采样
- Netty数据通信和心跳检测
- commons-pool冲突(jedis和dbcp)
- 将汉字转换为拼音
- 导航坐标系和大地坐标系的转化程序
- Qt 中QByteArray类的介绍和其方法的说明
- Intent的setClass和setClassName的区别
- 重拾java基础(五):相关知识说明
- QT学习笔记06常见对话框
- BUPT OJ 最小距离查询
- 子查询优化
- 放下的是你,放不下的是故事