java汉字转换拼音
1、汉字转换拼音首先引入一个叫pinyin4j-2.5.0.jar
下载地址:http://download.csdn.net/detail/yao__shun__yu/4670228
2、测试代码
import demo.Pinyin4jAppletDemo;
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 T {
public static void main(String[] args) throws BadHanyuPinyinOutputFormatCombination {
char[] ch = "平淡".toCharArray();
HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
/**
* 拼音大小写类型
*/
// t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);//小写拼音
// t3.setCaseType(HanyuPinyinCaseType.UPPERCASE);//大写拼音
/**
* 拼音输出方式
*/
// t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);//不输出语气,就是几声
t3.setToneType(HanyuPinyinToneType.WITH_TONE_NUMBER);//数字方式输出几声
// t3.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);//符号方式的输出几声
/**
* 拼音字母样式
*/
//The option indicates that the output of 'ü' is "v"
// t3.setVCharType(HanyuPinyinVCharType.WITH_V);
//The option indicates that the output of 'ü' is "ü" in Unicode form
// t3.setVCharType(HanyuPinyinVCharType.WITH_U_AND_COLON);
//The option indicates that the output of 'ü' is "u:"
// t3.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
System.out.println(ch);
//不加样式
String[] pinyinHead = PinyinHelper.toHanyuPinyinStringArray(ch[0]);
for(String str:pinyinHead){
System.out.println(str);
}
//加样式
pinyinHead = PinyinHelper.toHanyuPinyinStringArray(ch[1],t3);
for(String str:pinyinHead){
System.out.println(str);
}
}
}
运行结果:
平淡
ping2
dan4
标准案例:
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;
}
// -------------------------灏嗗瓧绗︿覆杞Щ涓篈SCII鐮�----------------------------
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("东软帝国"));
// 杩斿洖鎷奸煶鐨凙SCII
}
}
运行结果
dongruandiguo
drdg
e4b89ce8bdafe5b89de59bbd
java汉字转换拼音相关推荐
- java 汉字转换拼音
java 汉字转换拼音 maven依赖 <dependency><groupId>com.belerweb</groupId><artifactId>p ...
- Java汉字转换拼音工具类
1. 使用pinyin4j 1.1 引入相关maven依赖 <dependency><groupId>com.belerweb</groupId><artif ...
- java汉字转换拼音,获取汉字串拼音首字母
首先要下载 pinyin4j 的jar包 import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j ...
- java 汉字转换全拼、首字母拼音
最近接到需求,要求商城中进行半模糊搜索时,输入拼音字母同样要求进行自动匹配出相关品牌.类目.关键词等数据,从而进行筛选商品 本片内容只记录汉字转换拼音 方案一:在mysql数据库中编写函数,此方法存在 ...
- java 汉字转拼音_java中将汉字转换成拼音的实现代码
一:演示: 如在控制台输入:北京欢迎你 打印出来的拼音:bei jing huan ying ni 二:导入要依赖的jar: 三:代码编写 public static String get ...
- java汉字转换为拼音
java实现汉字转换拼音可以利用pinyin4j实现 1.加jar包: <dependency><groupId>com.belerweb</groupId>< ...
- Android 汉字转换拼音之JPinyin
一.简介 项目地址:https://github.com/stuxuhai/jpinyin JPinyin是一个汉字转拼音的Java开源类库,在PinYin4j的功能基础上做了一些改进. [JPiny ...
- java汉字转拼音工具类源代码
原文:java汉字转拼音工具类源代码 源代码下载地址:http://www.zuidaima.com/share/1550463387880448.htm 汉字转拼音 Pinyin pinyin = ...
- 昵称到拼音php,php 汉字转换拼音程序_PHP教程
php 汉字转换拼音程序 $p=ord(substr($str,$i,1)); if($p>160){ $q=ord(substr($str,++$i,1)); $p=$p*256+$q-655 ...
最新文章
- python 文件更新_使用Python批量更新服务器文件【新手必学】
- python视频教程从入门到精通全集-python从入门到精通视频(全60集)免费高速下载...
- 【风险管理】(第一篇)风险管理核心指标
- 信用风险模型(申请评分、行为评分)与数据准备(违约期限、WOE转化)
- Cracking the coding interview--Q1.5
- linux图片添加滤镜,PhotoFlare开源图像和照片编辑器,附在Ubuntu 18.04下的安装方法...
- 1.1 编程语言介绍
- 从 Hive 大规模迁移作业到 Spark 在有赞的实践
- 谷歌10月15日发布 Pixel 4;高通以31亿美元收购与TDK公司权益;甲骨文、VMware就云技术及支持达成协议……...
- 法拉克机器人自动怎么调_在使用钢网印刷SMT贴片红胶时全自动印刷机的参数怎么调?...
- Docker是什么,有什么用?一看就明白
- linux下MySQL安装及设置
- 分布式文件系统(FastDFS+Tengine+fastdfs-nginx-module)
- 为什么人人都应该学编程?
- android java 调试快捷键_最强Android studio 使用快捷键和调试技巧
- SketchUp:SketchUp草图大师经典案例之实现柜子、书桌等设计
- html在线post提交,HTML 两种方法实现post
- ajax xmlhttp下open方法POST、GET参数的区别
- 现代漫威联名钢铁侠车型开售 售价21.55万元起
- 微信 第三方开放平台 获取小程序授权并绑定小程序到开放平台(都是坑,留下帮助后人) 一