使用PinYin4j.jar将汉字转换为拼音
package com.Test.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; public class TestPinYin4j {
/**
* 将汉字转换为全拼
*
* @param src
* @return String
*/
public static String getPinYin(String src) {
char[] t1 = null;
t1 = src.toCharArray();
String[] t2 = new String[t1.length];
// 设置汉字拼音输出的格式 HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
t3.setVCharType(HanyuPinyinVCharType.WITH_V);
String t4 = "";
int t0 = t1.length;
try {
for (int i = 0; i < t0; i++) {
// 判断能否为汉字字符 // System.out.println(t1[i]); if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);// 将汉字的几种全拼都存到t2数组中 t4 += t2[0];// 取出该汉字全拼的第一种读音并连接到字符串t4后 } else {
// 如果不是汉字字符,间接取出字符并连接到字符串t4后 t4 += Character.toString(t1[i]);
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
return t4;
} /**
* 提取每个汉字的首字母
*
* @param str
* @return String
*/
public static String getPinYinHeadChar(String str) {
String convert = "";
for (int j = 0; j < str.length(); j++) {
char word = str.charAt(j);
// 提取汉字的首字母 String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
if (pinyinArray != null) {
convert += pinyinArray[0].charAt(0);
} else {
convert += word;
}
}
return convert;
} /**
* 将字符串转换成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) {
String cnStr = "鑫苑名家 中国人民银行 ";
System.out.println(getPinYin(cnStr));
System.out.println(getPinYinHeadChar(cnStr));
System.out.println(getCnASCII(cnStr));
}
}
原文:http://www.cnblogs.com/weidingqiang/p/4962404.html
使用PinYin4j.jar将汉字转换为拼音的更多相关文章
- JAVA实现汉字转换为拼音 pinyin4j/JPinyin
在项目中经常会遇到需求用户输入汉字后转换为拼音的场景,比如说通讯录,就会要求按名字首字符发音排序,如果自己写实现这方面的功能是个很好大的工程,还好网上有公开的第三方jar支持转换,结合网上很多前辈的代 ...
- 通过pinyin4j.jar将(汉字拼音混合字符串)转化成字母首字母
通过pinyin4j.jar将(汉字拼音混合字符串)转化成字母首字母 例如 我的中国心 ==> wdzgx 我的中国心ya ==> wdzgxya woai我的中国 ==> w ...
- iOS将汉字转换为拼音
将汉字转换为拼音 - (NSString *)chineseToPinyin:(NSString *)chinese withSpace:(BOOL)withSpace { CFStringRef h ...
- 使用CFStringTransform将汉字转换为拼音
之前做通讯录相关的一些App时,有一个比较常用的算法是将汉字转换成拼音.当时采用的做法是:将各个拼音段的首个汉字(按Unicode排序)做成两个数组,一个数组存拼音,另一个数组存拼音对应首个汉字的Un ...
- sql函数:汉字转换为拼音
sql数据库自定义一个函数把下面代码写进去 功能是得到汉字拼音首字母create function fun_getPY(@str nvarchar(4000)) returns nvarchar(40 ...
- 汉字转换为拼音的JavaScript库
将JSPinyin剥离mootools这个JavaScript库,可以独立使用. 1)一个是将汉字翻译为拼音,其中每一个字的首字母大写: pinyin.getFullChars(this.value) ...
- js汉字转换为拼音
片段 1 片段 2 gistfile1.txt /* --- description: Pinyin, to get chinese pinyin from chinese. license: MIT ...
- [pinyin4j] java版汉字转换拼音(大小写)
pinyin4J 是一个可以将汉字转换成拼音的lib,非常实用,其maven地址为:http://mvnrepository.com/artifact/com.belerweb/pinyin4j/2. ...
- 利用pingyin4j 将汉字转换为拼音
jar包:http://pan.baidu.com/s/11ikCY import net.sourceforge.pinyin4j.PinyinHelper; import net.sourcefo ...
随机推荐
- windows上安装RabbitMQ
windows下 安装 rabbitMQ 及操作常用命令 rabbitMQ是一个在AMQP协议标准基础上完整的,可服用的企业消息系统.它遵循Mozilla Public License开源协议,采用 ...
- readv writev示例程序
当 readv() 时候,需要程序自己提供space,接收数据. #include <stdio.h> #include <stdlib.h> #include <str ...
- djiango控制语句
{# 从0开始的索引#} {% for foo in value %} {# 从0开始的索引#} <p>{{ forloop.counter0 }}: {{ foo }}</p> ...
- Python 反射机制
Python的反射机制 Python的反射机制,就是反射就是通过字符串的形式,导入模块:通过字符串的形式,去模块寻找指定函数,并执行.利用字符串的形式去对象(模块)中操作(查找/获取/删除/添加)成员 ...
- 关键词提取_textbank
脱离语料库,仅对单篇文档提取 (1) pageRank算法:有向无权,平均分配贡献度 基本思路: 链接数量:一个网页越被其他的网页链接,说明这个网页越重要 链接质量:一个网页被一个越高权值的网页链接, ...
- maven install 找不到符号问题
看报错信息是找不到 javax.servlet 包 .这个是tomcat 内的jar包.但是我build path 查看是加了tomcat 的.. 最后在pom.xml 添加依赖 <depend ...
- 编译https://github.com/CIR-KIT/steer_drive_ros时出现的问题
解决gazebo对应的protobuf版本问题: I've come across to the same problem. I'm using Ubuntu 16.04, ROS Kinetic a ...
- python包与模块导入
一 .module 通常模块为一个文件,直接使用import来导入就好了.可以作为module的文件类型有".py".".pyo".".pyc&quo ...
- Hibernate validator的一些额外特性
分组验证及分组顺序 如果我们想在新增的情况验证id和name,而修改的情况验证name和password,怎么办? 那么就需要分组了. 首先定义分组接口://分组接口就是两个普通的接口,用于标识,类似 ...
- Java语法基础课 原码 反码 补码
原码就是符号位加上真值的绝对值, 即用第一位表示符号, 其余位表示值. 反码的表示方法是:正数的反码是其本身:负数的反码是在其原码的基础上, 符号位不变,其余各个位取反. 补码的表示方法是在反码的基础 ...