引入依赖

 <!--添加汉字首字母的jar包--><dependency><groupId>com.belerweb</groupId><artifactId>pinyin4j</artifactId><version>2.5.0</version></dependency>

代码

package com.ekkcole.controlle;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;import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;import static jdk.nashorn.internal.objects.Global.undefined;public class HanziZhuanPinyin {public static void main(String[] args) throws Exception {// TODO Auto-generated method stubSystem.out.println(ToFirstChar("喜马拉雅山").toUpperCase()); //转为首字母大写System.out.println(ToPinyin("喜马拉雅山")); // 转为小写拼音System.out.println(Setnumber("喜马拉雅山"));  // 转为拼音首字母大写}/*** 获取字符串拼音的第一个字母** @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 Setnumber(String SetMealName) throws Exception {//实例化HanyuPinyinOutputFormat对象HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
//设置字母类型属性format.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 拼音小写format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 拼音不标声调format.setVCharType(HanyuPinyinVCharType.WITH_V);// u:的声母替换为vStringBuilder sb = new StringBuilder();try {//首写大写字母for (int i = 0; i < SetMealName.length(); i++) {String[] array = PinyinHelper.toHanyuPinyinStringArray(SetMealName.charAt(i), format); //转换为每个字符拼音数组集合if (array == null || array.length == 0) {//判断字符串是否为空continue; //停止执行}String s = array[0];// 不管多音字,只取第一个char c = s.charAt(0);// 大写第一个字母String pinyin = String.valueOf(c).toUpperCase().concat(s.substring(1));//首写大写sb.append(pinyin);//拼接}} catch (BadHanyuPinyinOutputFormatCombination e) {e.printStackTrace();}return sb.toString();}}

中文汉字转拼音首字母大写相关推荐

  1. PHP将中文字符转为拼音/首字母大写/或其他连接符

    PHP将中文字符转为拼音/首字母大写/或其他连接符 项目中遇到需要将客户的姓名转为首拼并且大写的要求, 提出解决办法 首先建立一个汉字转拼音的类 class PinYin {private $pyli ...

  2. C# 汉字转拼音首字母大写

    我们在手机上经常都能看到通过拼音缩写来搜索好友信息,那么它是如何将一段汉字转换为首字母大写呢 首先我们来了解一下中文在计算机中的结构.计算机识别的是字节,字节(Byte)是计算机信息技术用于计量存储容 ...

  3. java 汉字 字母_JAVA获取中文汉字字符串拼音首字母,英文字符不变的工具类

    java在处理项目需求时,有时需要排序,尤其是中文名字按0-9,A-Z进行首字母排序,这时候就需要首先要得到中文字符串的首字母,然后按照字母顺序进行排序,不多说,直接上代码,看java如何获取中文字符 ...

  4. JS输入中文自动获取拼音首字母大写

    输入中文时,自动获取首字母大写 1.安装插件pinyin-pro npm install pinyin-pro 2.引入 import { pinyin } from 'pinyin-pro'; 3. ...

  5. 中文汉字转拼音首字母

    Utils: public class HanZiConverterPinYinUtil {public HanZiConverterPinYinUtil() {// 初始化汉字拼音字母对照表init ...

  6. ORACLE SQL函数中文汉字转拼音首字母

    CREATE OR REPLACE FUNCTION FUN_GET_PYJM(P_NAME IN VARCHAR2) RETURN VARCHAR2 ASV_COMPARE VARCHAR2(100 ...

  7. 在excel中实现汉字转换拼音首字母大写

    分享一下我老师大神的人工智能教程.零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow 在excel 20 ...

  8. java将汉字转成拼音首字母大写字母_java 根据汉字生成拼音全拼或拼音首字母的示例...

    1.情景展示 java 根据中文生成对应的拼音 2.准备工作 所需jar包:pinyin4j-2.5.0.jar 3.解决方案 导包 import net.sourceforge.pinyin4j.P ...

  9. 根据汉字获取它的字符串拼音首字母(大写),含多音字

    /// <summary>         /// 根据汉字获取它的字符串拼音首字母(大写),含多音字         /// </summary>         /// & ...

  10. 如何让中文转换成其拼音首字母大写

    作者:杨裙 本次任务完成时间:2019年2月14日 开发工具与关键技术:Visual Studio 2015 .SQL Server 2014 Management Studio.c#/MVC 一.让 ...

最新文章

  1. Spring Boot 属性配置和使用
  2. 单高斯分布模型GSM,高斯混合模型GMM
  3. 营销获客场景下的工具类产品规划
  4. 96KB存储器的怎么算地址范围_产品条码怎么申请费用
  5. 计算机专业英语critical,计算机专业英语教程汇总.ppt
  6. 编辑中的word变成只读_文档设定密级,word中是这样加密,看一篇就精通了
  7. 15、Web安全测试之XSS
  8. 在ASP.NET下做了一个实验MVC的小东西,希望能得到更多的参考意见
  9. 破解密码——利用Windows PE操作系统破解Windows PIN
  10. 如何理解UEFI的事件机制(三)——时钟中断
  11. 21. SCHEMATA
  12. 计算机知识技能大赛总结,计算机知识技能大赛总结
  13. python seaborn教程_Seaborn官方教程中文教程(一)
  14. 文字转语音,有什么软件好用?
  15. 教你怎样混社会[转]
  16. 常用Source Insight快捷键
  17. maya python插件_Maya Python - ALembic导入导出助手
  18. yoloV5 教程——tensorboard使用
  19. 安利一下断言利器AssertJ
  20. 视频教程-JavaScript实战讲解课程-Java

热门文章

  1. 安卓6.0系统一键激活XPOSED框架的方法
  2. es的插件 ik分词器的安装和使用
  3. 盘点微软CEO纳德拉十大有趣事实
  4. 《用计算机写作文》说课稿,《我用电脑写作文》说课稿
  5. DP转HDMI音视频数据转换器普瑞PS176方案设计
  6. 【补丁】YYC松鼠短视频系统补丁,增加视频点赞数据管理功能,可修改点赞数量,V2.8的功能
  7. ​说了你可能不信,这是一份王者荣耀接口文档
  8. 安科瑞电力监控系统在教学演示中的应用
  9. Scala学习笔记(1)-基本类型归纳
  10. emoji表情 mysql转移,mysql中emoji表情存储