需要加入的jar包

     <!-- pinyin4j    --><dependency><groupId>com.belerweb</groupId><artifactId>pinyin4j</artifactId><version>2.5.1</version></dependency>

编写工具类

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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 pinyin4jUtil{/*** 获取汉字串拼音首字母,英文字符不变* @param chinese 汉字串* @return 汉语拼音首字母*/public static String getFirstSpell(String chinese) {StringBuffer pybf = new StringBuffer();char[] arr = chinese.toCharArray();HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);for (int i = 0; i < arr.length; i++) {if (arr[i] > 128) {try {String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);if (temp != null) {pybf.append(temp[0].charAt(0));}} catch (BadHanyuPinyinOutputFormatCombination e) {e.printStackTrace();}} else {pybf.append(arr[i]);}}return pybf.toString().replaceAll("\\W", "").trim();}/*** 获取汉字串拼音,英文字符不变* @param chinese 汉字串* @return 汉语拼音*/public static String getFullSpell(String chinese) {StringBuffer pybf = new StringBuffer();char[] arr = chinese.toCharArray();HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);for (int i = 0; i < arr.length; i++) {if (arr[i] > 128) {try {pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]);} catch (BadHanyuPinyinOutputFormatCombination e) {e.printStackTrace();}} else {pybf.append(arr[i]);}}return pybf.toString();}public static void main(String[] args) throws BadHanyuPinyinOutputFormatCombination {System.out.println(getFirstSpell("测试"));System.out.println(getFirstSpell("运行"));System.out.println(getFirstSpell("Mike"));System.out.println("---------------------------");System.out.println(getFullSpell("测试"));System.out.println(getFullSpell("运行"));System.out.println(getFullSpell("Mike"));}}

运行结果

java汉字转拼音和获取汉语拼音首字母相关推荐

  1. php 将中文字符转英文字母_PHP实现将汉字转换为拼音及获取词语首字母的方法...

    本文实例讲述了PHP实现将汉字转换为拼音及获取词语首字母的方法.分享给大家供大家参考,具体如下: 最近要开发将汉字转换为拼音和得到首字的功能记录下来哈子:呵呵~ class Pinyin{ priva ...

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

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

  3. php将汉字转换为拼音和得到词语首字母(一)

    <?php/** * 修复二分法查找方法 * 汉字拼音首字母工具类 * 注: 英文的字串:不变返回(包括数字) eg .abc123 => abc123 * 中文字符串:返回拼音首字符 e ...

  4. mysql 字母分组_MySQL按照汉字的拼音排序、按照首字母分类

    项目中有时候需要按照汉字的拼音排序,比如联系人列表.矿物分类等,有的还需要按拼音字母从A到Z分类显示. 如果存储汉字的字段编码使用的是GBK字符集,因为GBK内码编码时本身就采用了拼音排序的方法(常用 ...

  5. php将汉字转换为拼音和得到词语首字母

    最近要开发将汉字转换为拼音和得到首字的功能记录下来哈子:呵呵~ class Pinyin{private $_outEncoding = "GB2312";public funct ...

  6. uni-app 汉字转拼音 搜索和按首字母排序页面

    <template><view class="outer"><view class="search"><view> ...

  7. mysql转拼音首字母大写_Mysql中文汉字转拼音的实现  mysql首字母转化为大写

    这篇文章主要介绍了Mysql中文汉字转拼音的实现,并且每个汉字会转换全拼,使用Mysql自定义函数实现,需要的朋友可以参考下 第一步:我们将在mysql中新建必要的函数 一.创建拼音对照表 -- 创建 ...

  8. Java获取名字首字母拼音并按名字拼音分组工具类

    1. maven <dependency><groupId>com.belerweb</groupId><artifactId>pinyin4j< ...

  9. java汉字转拼音以及得到首字母通用方法

    首先需要引入一个jar包:pinyin4j-2.5.0.jar  链接: https://pan.baidu.com/s/1BJL7KvuJ7AXhSKQUloEi_g 提取码: v39m packa ...

最新文章

  1. git 下载 github 上的代码
  2. SAP MM 事务代码RWBE查询库存
  3. tools.jar seem to ....
  4. RTP:实时应用程序传输协议
  5. 22个激活函数,ReLU、RReLU、LeakyReLU、PReLU、Sofplus、ELU、CELU、SELU、GELU、ReLU6、Sigmoid、Tanh、Softsign、Hardtanh等
  6. 来,看看这20个常用的宏定义!
  7. win10+tensorflow faster-RCNN 训练自己的数据集
  8. 面试官,再也别问我的系统如何支持高并发了
  9. Tensorflow并行计算:多核(multicore),多线程(multi-thread),计算图分割(Graph Partition)
  10. 使用peewee增删查改数据库
  11. haproxy 配置 说明
  12. linux系统剪切,Linux 系统裁剪
  13. 汽车VIN码超详细解析规则
  14. 虚拟化: 物理CPU与VCPU的关系 || 内存上限说起 VMware内存分配初探
  15. Borg和Kubernetes有什么不同?未来的云需要什么?
  16. 如何高效的利用谷歌学术搜索文献
  17. 微信扫二维码跳转网页链接如何制作
  18. Pixhawk学习9——固定翼位置控制(L1控制+TECS总能量控制)
  19. 超级计算机通信工程研发相关高校,[现场]世界大学生超级计算机竞赛ASC的第八年...
  20. 模电(八)放大电路静态工作点的稳定性

热门文章

  1. 智伴机器人三级分销模式_三级分销系统的模式有哪些
  2. 2019尚硅谷大数据Maven篇一 Maven安装和概念
  3. 复合赋值运算符“+=、-=、*=、/=、%=”详解
  4. Android 10 SystemUI 如何添加4G信号和WiFi图标
  5. style.left和offsetLeft 用法
  6. php面试-职业规划
  7. 怎么将pdf转换excel转换器
  8. QByteArray详解(qt)
  9. 2023 HGAME网络攻防大赛wp
  10. excel选择性粘贴为何是html,Excel选择性粘贴预览有什么功能