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汉字转换拼音相关推荐

  1. java 汉字转换拼音

    java 汉字转换拼音 maven依赖 <dependency><groupId>com.belerweb</groupId><artifactId>p ...

  2. Java汉字转换拼音工具类

    1. 使用pinyin4j 1.1 引入相关maven依赖 <dependency><groupId>com.belerweb</groupId><artif ...

  3. java汉字转换拼音,获取汉字串拼音首字母

    首先要下载  pinyin4j  的jar包 import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j ...

  4. java 汉字转换全拼、首字母拼音

    最近接到需求,要求商城中进行半模糊搜索时,输入拼音字母同样要求进行自动匹配出相关品牌.类目.关键词等数据,从而进行筛选商品 本片内容只记录汉字转换拼音 方案一:在mysql数据库中编写函数,此方法存在 ...

  5. java 汉字转拼音_java中将汉字转换成拼音的实现代码

    一:演示:       如在控制台输入:北京欢迎你 打印出来的拼音:bei jing huan ying ni 二:导入要依赖的jar: 三:代码编写 public static String get ...

  6. java汉字转换为拼音

    java实现汉字转换拼音可以利用pinyin4j实现 1.加jar包: <dependency><groupId>com.belerweb</groupId>< ...

  7. Android 汉字转换拼音之JPinyin

    一.简介 项目地址:https://github.com/stuxuhai/jpinyin JPinyin是一个汉字转拼音的Java开源类库,在PinYin4j的功能基础上做了一些改进. [JPiny ...

  8. java汉字转拼音工具类源代码

    原文:java汉字转拼音工具类源代码 源代码下载地址:http://www.zuidaima.com/share/1550463387880448.htm 汉字转拼音 Pinyin pinyin = ...

  9. 昵称到拼音php,php 汉字转换拼音程序_PHP教程

    php 汉字转换拼音程序 $p=ord(substr($str,$i,1)); if($p>160){ $q=ord(substr($str,++$i,1)); $p=$p*256+$q-655 ...

最新文章

  1. python 文件更新_使用Python批量更新服务器文件【新手必学】
  2. python视频教程从入门到精通全集-python从入门到精通视频(全60集)免费高速下载...
  3. 【风险管理】(第一篇)风险管理核心指标
  4. 信用风险模型(申请评分、行为评分)与数据准备(违约期限、WOE转化)
  5. Cracking the coding interview--Q1.5
  6. linux图片添加滤镜,PhotoFlare开源图像和照片编辑器,附在Ubuntu 18.04下的安装方法...
  7. 1.1 编程语言介绍
  8. 从 Hive 大规模迁移作业到 Spark 在有赞的实践
  9. 谷歌10月15日发布 Pixel 4;高通以31亿美元收购与TDK公司权益;甲骨文、VMware就云技术及支持达成协议……...
  10. 法拉克机器人自动怎么调_在使用钢网印刷SMT贴片红胶时全自动印刷机的参数怎么调?...
  11. Docker是什么,有什么用?一看就明白
  12. linux下MySQL安装及设置
  13. 分布式文件系统(FastDFS+Tengine+fastdfs-nginx-module)
  14. 为什么人人都应该学编程?
  15. android java 调试快捷键_最强Android studio 使用快捷键和调试技巧
  16. SketchUp:SketchUp草图大师经典案例之实现柜子、书桌等设计
  17. html在线post提交,HTML 两种方法实现post
  18. ajax xmlhttp下open方法POST、GET参数的区别
  19. 现代漫威联名钢铁侠车型开售 售价21.55万元起
  20. 微信 第三方开放平台 获取小程序授权并绑定小程序到开放平台(都是坑,留下帮助后人) 一

热门文章

  1. C++日期和时间编程总结
  2. 华三防火墙web端口_H3C SecPath F100-C-AI防火墙 Web配置指导-5PW100
  3. 知识图谱和语义网概述
  4. 虎书第五版中文翻译 2.4 向量
  5. django系统学习笔记
  6. django图书管理系统:
  7. 洛谷P1002过河卒
  8. (番外)申根国家签证办理经验分享(仅供在校学生参考,特别是南七技校的学生)
  9. [深度学习从入门到女装]keras实战-Unet3d(BRAST2015)
  10. Android uni-app 封装原生插件