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 yong.chen 2013-04-07
*
*/
public class Pinyin4jUtil {
/**
* 将汉字转换为全拼
*
* @param src
* @return String
*/
public static String getPinYin(String src) {
char[] t1 = null;
t1 = src.toCharArray();
// System.out.println(t1.length);
String[] t2 = new String[t1.length];
// System.out.println(t2.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++) {
// System.out.println(Integer.toHexString(bGBK[i] & 0xff));
// 将每个字符转换成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));
}
}
相关推荐
网上传的postgreSQL汉字转拼音的方法,是针对9.3的版本(相信你要是8.4的也搜到了但不能直接用,才来看我的),尝试了8.4的版本来做拼音处理。发现网上的《postgreSQL汉字转拼音》里面坑比较多,尝试纠正了一下,发...
C++,采用“拼音-中文”的数据字典的方式实现中文汉字转拼音,兼容多音字,字典为txt,后期可拓展易维护。在STL的map中查找效率16毫秒左右。
主要介绍了Mysql中文汉字转拼音的实现,并且每个汉字会转换全拼,使用Mysql自定义函数实现,需要的朋友可以参考下
C#汉字转拼音(npinyin)将中文转换成拼音全文或首字母
// 简单的高效的汉字转拼音、拼音转汉字类 // 包含20441个汉字26821拼音转换库文件,字库基本是网上最全的了,当然万一发现少了可以自己加入到字库中,一行一条记录加入到文本文件中即可,非常简单 // 算法方面没有...
支持2万+汉字转拼音
java汉字转拼音,android汉字转拼音,汉字转拼音首字母,汉字转ASCII
Java汉字转拼音(支持多音字)
Qt C++ 汉字转拼音与首字母,用Qt 5.5亲测可用
.NET实现汉字转拼音,汉字转拼音,语言转换 .NET实现汉字转拼音,汉字转拼音,语言转换 .NET实现汉字转拼音,汉字转拼音,语言转换 .NET实现汉字转拼音,汉字转拼音,语言转换
SQL中将汉字转换成拼音,这里是转换汉字首拼音,如果需要全部转换,在以下程序中稍微改几个数字就可以了
中文转拼音的js控件,引用非常简单。
汉字转拼音声母首字母 dll delphi
汉字转拼音 asp 文件汉字转拼音 asp 文件汉字转拼音 asp 文件
Java 中文转拼音/中文转五笔/汉字转拼音/汉字转五笔 字典. 通过 https://blog.csdn.net/qcl108/article/details/100981774 提供的 Java代码使用.
sql 汉字转拼音首字母 代码 function --汉字转拼音首字母sql Create FUNCTION dbo.fnpbGetPYFirstLetter ( @str NVARCHAR(4000) )
文件里面已弄了使用说明,转换比较全面,已经开发和测试。可用于后台的URL的路径使用,以及其它方面的一些转换使用,详情自己挖掘...
Java实现汉字转拼音功能,包括首字母和全拼,为大家减少时间,为此提供便捷。
比较全的汉字转拼音,可以转2万多个汉字的,自己根据拼音字库编写的,花了很多心思的。
网上查询了很多汉字转拼音的方案,一种是用VBA实现的,另一种是只能转汉字首字母,都不是我需要的方案,因此结VBA的方式,用Delphi实现汉字转拼音全拼的方法,对excel文件中的中文转成拼音后,写入excel其他列中,并...