Java汉字转换拼音工具类
1. 使用pinyin4j
1.1 引入相关maven依赖
<dependency><groupId>com.belerweb</groupId><artifactId>pinyin4j</artifactId><version>2.5.0</version></dependency>
1.2 汉字转换拼音工具类相关代码
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;/*** @description: 汉字拼音转换* @author: zyb* @date: 2020/01/13 15:40*/
public class PinyinUtils {/*** 获得汉字拼音首字母* @param chines 中文* @return 汉字拼音首字母*/public static String getAlpha(String chines) {String pinyinName = "";char[] nameChar = chines.toCharArray();HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);for (int i = 0; i < nameChar.length; i++) {if (nameChar[i] > 128) {try {pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0].charAt(0);} catch (BadHanyuPinyinOutputFormatCombination e) {e.printStackTrace();}} else {pinyinName += nameChar[i];}}return pinyinName;}/*** 将字符串中的中文转化为拼音,英文字符不变* @param inputString 字符串* @return 全英文字符串*/public static String getPingYin(String inputString) {HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();format.setCaseType(HanyuPinyinCaseType.LOWERCASE);format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);format.setVCharType(HanyuPinyinVCharType.WITH_V);String output = "";if (inputString != null && inputString.length() > 0&& !"null".equals(inputString)) {char[] input = inputString.trim().toCharArray();try {for (int i = 0; i < input.length; i++) {if (Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")) {String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);output += temp[0];} elseoutput += Character.toString(input[i]);}} catch (BadHanyuPinyinOutputFormatCombination e) {e.printStackTrace();}} else {return "*";}return output;}public static void main(String[] args) {String englishStr = PinyinUtils.getPingYin("测试123ceshi");String s = PinyinUtils.getAlpha("测试123ceshi");System.out.println(englishStr);System.out.println(s);}
}
Java汉字转换拼音工具类相关推荐
- java汉字转拼音工具类源代码
原文:java汉字转拼音工具类源代码 源代码下载地址:http://www.zuidaima.com/share/1550463387880448.htm 汉字转拼音 Pinyin pinyin = ...
- Java汉字转为拼音工具类
依赖文件 <!-- https://mvnrepository.com/artifact/com.belerweb/pinyin4j --><dependency><gr ...
- Java汉字转拼音工具类(支持首字母和全拼)
工具类产生是因为个人业务需求需要根据中文汉字排序,而博主又对网上回答不满意,所以才根据相关资料写了该工具类,写入博客 以备不时之需.直接上代码: Java汉字转成汉语拼音工具类,需要用到pinyin4 ...
- Java 中文转拼音工具类 (附带长度转换 2:1)
Java 中文转拼音工具类 (附带长度转换 2:1) import com.google.common.collect.Lists; import com.google.common.collect. ...
- 汉字转拼音(工具类)
2019独角兽企业重金招聘Python工程师标准>>> package com.qst.tesc.course.web.rest.util; import java.io.Unsup ...
- php 汉字转拼音类,PHP汉字转换拼音的类_php
网络上类似的代码大多只能在gb2312编码下使用,下面这个类同时能在utf-8编码下将汉字转换为拼音,具体的代码和用法如下: function Pinyin($_String, $_Code='gb2 ...
- java 汉字转换拼音
java 汉字转换拼音 maven依赖 <dependency><groupId>com.belerweb</groupId><artifactId>p ...
- 汉字转拼音工具类,依赖Pinyin4J
汉字转拼音工具类,依赖Pinyin4J Maven 坐标 <dependency><groupId>com.belerweb</groupId><artifa ...
- Java汉字转汉语拼音工具类
Java汉字转成汉语拼音工具类,需要用到pinyin4j.jar包,我使用的是Gradle项目,在build.gradle中引入依赖 // https://mvnrepository.com/arti ...
最新文章
- 几个比较冷门的prototype扩展方法:去掉html标签、去掉a标签、去掉style样式,以及判断是否为html代码的代码片段
- HP服务器选型一般标准
- BZOJ 2154 [国家集训队]Crash的数字表格 / JZPTAB(莫比乌斯反演,经典好题)(Luogu P1829)
- 一次mysql瘫痪解救
- 再次召唤灵魂画手!这个AI把你信手涂鸦都变成人脸
- 使用PaddleFluid和TensorFlow实现图像分类网络SE_ResNeXt | 文末超大福利
- ITK:自定义操作以对应两个图像中的像素
- 使用sphinx搜索子域名过程中需注意的问题
- Go unsafe Pointer
- python计算方位角_实例讲解:用python 计算方位角(根据两点的坐标计算)记得收藏哦...
- 【HDU - 5884】Sort(k叉哈夫曼树,优化tricks,两个队列代替优先队列)
- J.U.C之并发工具类:Exchanger
- [POJ1664] 放苹果 (动态规划,组合数学)
- python配置文件封装_Python configparser模块封装及构造配置文件代码示例
- 对于J2EE体系架构的理解
- 动作捕捉在运动科学领域的应用
- Skeleton Screen -- 骨架屏
- 流体力学(量纲分析和流动相似原理)
- 常见网络钓鱼攻击类型
- VB6.0 google 地图显示GPS数据
热门文章
- MATLAB | 一文解决各类曲面交线绘制,包含三维隐函数曲面交线
- Open3D 曲面重建
- vue 中deep使用
- WEBSHELL姿势之SQL一句话
- vscode vuejs项目import报错找不到模块“@/assets/image/BackGroun.png”或其相应的类型声明。
- 基于eNSP华为模拟器的VRRP简单实验
- android谷歌dns设置在哪,安卓手机怎么设置DNS Android手机修改DNS图文教程
- 阿里性能专家全方位对比Jmeter和Locust,到底谁更香?
- hd530黑苹果硬解_解决黑苹果HD3000核显 VGA和HDMI外接显示器无反应问题
- [APIO2014]连珠线