有时候为了方便操作程序的开发,需要将汉字转为拼音等操作。下面这个是自己结合网上的资料,加上自己在公司项目中的亲自实践。完整的实现了将汉字转为拼音的操作。这个Demo只是负责将其转换,在main方法中测试,在实际需要中,只需要调用这个类中的方法即可。

   首先贴出测试结果:

      

   测试参数:

        汉字转换为拼音

        汉字转换为拼音

     main测试方法的代码:

public static void main(String[] args) {System.out.println(ToFirstChar("汉字转换为拼音").toUpperCase()); //转为首字母大写System.out.println(ToPinyin("汉字转换为拼音"));
}

本功能的实现时利用java开源库,开发此程序需要一个jar包。本人用的是pinyin.jar。网上可以直接下载,也可以在其官网进行下载。在此不祥述。如果实在不乐意,可以点击下载将进行这个jar包的下载。

  贴出实现该Demo的源码:

package com.red.test;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.exception.BadHanyuPinyinOutputFormatCombination;/*** 汉字转换为拼音* @author Red*/
public class PinyinDemo {/*** 测试main方法* @param args*/public static void main(String[] args) {System.out.println(ToFirstChar("汉字转换为拼音").toUpperCase()); //转为首字母大写System.out.println(ToPinyin("汉字转换为拼音")); }/*** 获取字符串拼音的第一个字母* @param chinese* @return*/public static String ToFirstChar(String chinese){         String pinyinStr = "";  char[] newChar = chinese.toCharArray();  //转为单个字符HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);  defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  for (int i = 0; i < newChar.length; i++) {  if (newChar[i] > 128) {  try {  pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0);  } catch (BadHanyuPinyinOutputFormatCombination e) {  e.printStackTrace();  }  }else{  pinyinStr += newChar[i];  }  }  return pinyinStr;  }  /*** 汉字转为拼音* @param chinese* @return*/public static String ToPinyin(String chinese){          String pinyinStr = "";  char[] newChar = chinese.toCharArray();  HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();  defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);  defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  for (int i = 0; i < newChar.length; i++) {  if (newChar[i] > 128) {  try {  pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];  } catch (BadHanyuPinyinOutputFormatCombination e) {  e.printStackTrace();  }  }else{  pinyinStr += newChar[i];  }  }  return pinyinStr;  }
}

补充:查询拼音全拼并包含音调

 public static String getPinYin(String src) {char[] t1 = null;t1 = src.toCharArray();String[] t2 = new String[t1.length];//t3是全部的拼音,不带声调HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);t3.setVCharType(HanyuPinyinVCharType.WITH_V);//format是全部的拼音并且带声调HanyuPinyinOutputFormat format= new HanyuPinyinOutputFormat();format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);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);t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], format);t4 += t2[0]+" ";} else {t4 += java.lang.Character.toString(t1[i]);}}return t4;} catch (BadHanyuPinyinOutputFormatCombination e1) {e1.printStackTrace();}return t4;}

java实现将汉字转为拼音并包含音调相关推荐

  1. java实现将汉字转为拼音

    原文:java实现将汉字转为拼音 有时候为了方便操作程序的开发,需要将汉字转为拼音等操作.下面这个是自己结合网上的资料,加上自己在公司项目中的亲自实践.完整的实现了将汉字转为拼音的操作.这个Demo只 ...

  2. java实现汉字转为拼音

    java实现汉字转为拼音: 1.需要导入pinyin4j.jar package com.loo.pinyin; import net.sourceforge.pinyin4j.PinyinHelpe ...

  3. Java实用工具类-将汉字转为拼音

    1. 导入对应的jar包 <dependency><groupId>com.belerweb</groupId><artifactId>pinyin4j ...

  4. Java汉字转为拼音工具类

    依赖文件 <!-- https://mvnrepository.com/artifact/com.belerweb/pinyin4j --><dependency><gr ...

  5. 【实例】PHP如何实现汉字转为拼音的?

    php汉字转拼音本次使用的是 overtrue/pinyin 扩展.基于 CC-CEDICT 词典的中文转拼音工具,更准确的支持多音字的汉字转拼音解决方案. 一.安装 1.使用 Composer 安装 ...

  6. android中汉字转为拼音

    汉语转为拼音 首先要先导入架包:com.belerweb:pinyin4j:2.5.1 工具类: 注释的方法,是可以将单个汉字转为拼音的,为注释的可以转化词语, public class HanZiT ...

  7. python第三方库:pypinyin将汉字转为拼音

    汉字的拼音虽然有一定的规律,但是做一套好的汉字转拼音的系统并不是那么容易,需要考虑的问题也比较多.汉字转拼音在多个的方向上也经常使用到.比如在url中,很少使用中文作为url连接,一种方式是转换为拼音 ...

  8. Java将汉字转为拼音

    汉字转换成拼音 : 首先jar:pinyin4j-2.5.0.jar 或: <dependency><groupId>com.belerweb</groupId>& ...

  9. Java中将汉字转为拼音

    转载请标明出处:http://blog.csdn.net/liu1252247624/article/details/51553825 源码地址:http://download.csdn.net/de ...

  10. java hanyupinyinoutputformat();_Java汉字转拼音pinyin4j用法

    Java汉字转拼音pinyin4j用法 在学习编程的过程中,我觉得不止要获得课本的知识, 更多的是通过学习技术知识提高解决问题的能力,这样我们才能走在最前方,更多Java学习,请登陆疯狂java官网. ...

最新文章

  1. 比利时皇家科学院院士Luc De Raedt:从统计关系人工智能到神经符号计算
  2. Java连接Mysql数据库增删改查实现
  3. Keil uVision4 for ARM 下增加支持C51,C5x
  4. python优先级排序_python中使用优先队列
  5. 一种极端思维引起的幻觉
  6. 黑域助手连接服务器才能用吗,黑域app怎么使用?进入黑域详细教程[图]
  7. c语言中十六进制可以直接和十进制运算吗,C语言 · 十六进制转十进制
  8. MT【99】2005联赛二试题我的一行解法
  9. 洛谷P1195 口袋的天空
  10. 单元测试中 Right-BICEP 和 CORRECT
  11. 清华同方锋锐 u430 更换内存条教程
  12. plsql 客户端字符集_plsql查看数据库字符集
  13. 高新技术企业的申请条件
  14. Windows 7登陆时自动创建TEMP临时文件夹问题解决
  15. linux软件安装 home,Ubuntu中安装建筑设计软件Sweet Home 3D
  16. 计算机休眠策略,桌面从睡眠或休眠中意外唤醒
  17. 微信小程序实现pdf、word等格式文件上传的方法,微信小程序word文档
  18. 查看实时网速的小方法
  19. PAT乙级(Basic Level)练习题 客似云来
  20. 超实用的浏览器插件:CSDN全站去广告

热门文章

  1. Java项目——细胞自动机
  2. labelImg安装与操作
  3. SSD固态硬盘主控技术
  4. Linux发行版列表
  5. 华为鸿蒙专属文件后缀,华为鸿蒙——上传第三方APP【原理公布】
  6. Docker资源配置方法(Cgroup)
  7. scratch动态三角形拖动/自制素材/少儿编程scratch教研教案课件课程素材脚本
  8. 数据库:数据库设计与数据建模及建模工具(PowerDesigner)
  9. 好用的数据建模工具,探索中完善
  10. 用html制作学生个人博客,网页制作论坛(学生个人网页制作代码)