java代码将汉字转换成拼音
来源:互联网 发布:绿色 视力 知乎 编辑:程序博客网 时间:2024/05/01 15:26
本文来自 http://www.cn-java.com/www1/?action-viewnews-itemid-102374
package com.sucre;
//所需导入的jar包在下侧下载
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 ChineseToEnglish {
//------------------------将汉字转换为全拼-------------------------------
public static String getPingYin(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++){
//判断是否为汉字字符
if(java.lang.Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")){
t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
t4+=t2[0];
}else{
t4+=java.lang.Character.toString(t1[i]);
}
}
//System.out.println(t4);
return t4;
}catch (BadHanyuPinyinOutputFormatCombination e1){
e1.printStackTrace();
}
return t4;
}
//----------------------------------------返回中文的首字母--------------------
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码-----------------------------
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));
strBuf.append(Integer.toHexString(bGBK[i]&0xff));
}
return strBuf.toString();
}
//--------------------------测试拼音转汉字是否正确----------------------------
public static void main(String[] args){
System.out.println(getPingYin("西安市")); //返回拼音的全拼样式
System.out.println(getPinYinHeadChar("西安市")); //返回拼音的首字母
System.out.println(getCnASCII("西安市")); //返回拼音的ASCII
}
}
//所需导入的jar包在下侧下载
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 ChineseToEnglish {
//------------------------将汉字转换为全拼-------------------------------
public static String getPingYin(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++){
//判断是否为汉字字符
if(java.lang.Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")){
t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
t4+=t2[0];
}else{
t4+=java.lang.Character.toString(t1[i]);
}
}
//System.out.println(t4);
return t4;
}catch (BadHanyuPinyinOutputFormatCombination e1){
e1.printStackTrace();
}
return t4;
}
//----------------------------------------返回中文的首字母--------------------
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码-----------------------------
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));
strBuf.append(Integer.toHexString(bGBK[i]&0xff));
}
return strBuf.toString();
}
//--------------------------测试拼音转汉字是否正确----------------------------
public static void main(String[] args){
System.out.println(getPingYin("西安市")); //返回拼音的全拼样式
System.out.println(getPinYinHeadChar("西安市")); //返回拼音的首字母
System.out.println(getCnASCII("西安市")); //返回拼音的ASCII
}
}
- java代码将汉字转换成拼音
- java代码将汉字转换成拼音
- java代码将汉字转换成拼音
- java 学习:将汉字转换成拼音
- Java将汉字转换为拼音
- java将汉字转换为拼音
- java 汉字转换成拼音
- java 汉字转换成拼音
- Java 汉字转换成拼音
- java 汉字转换成拼音
- java中将汉字转换成拼音的实现代码
- 将汉字转换成拼音(php版)
- c#将汉字转换成拼音
- 使用pinyin4j将汉字转换成拼音
- 将中文汉字转换成拼音
- php将汉字转换成拼音
- js 如何将汉字转换成拼音
- php 将汉字转换成拼音
- GZip压缩
- android环境搭建(Myeclipse8.0).doc
- js清除cookie
- wince 编写服务程序
- CAS单点登录原理图
- java代码将汉字转换成拼音
- tail命令的使用
- 在vs2008中打开以前在vs2005上做的项目
- 每天学习一算法系列(29)(有两个序列a,b,大小都为n,序列元素的值任意整数,无序;要求:通过交换a,b 中的元素,使[序列a 元素的和]与[序列b 元素的和]之间的差最小)
- 单片机超声波测距模块学习笔记_发射端学习
- 扫描按键的解决方法
- Java的GC工作原理
- winCE的体系结构和功能
- maven打jar例子