将字符串中的中文转为拼音(java)
maven依赖
<dependency><groupId>com.belerweb</groupId><artifactId>pinyin4j</artifactId>
</dependency>
拼音的工具类
package top.codekiller.manager.common.utils;import lombok.extern.slf4j.Slf4j;
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;/*** @author codekiller* @date 2020/5/27 14:22*/
@Slf4j
public class PinYinUtil {/*** 将字符串中的中文转化为拼音,其他字符不变** @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);char[] input = inputString.trim().toCharArray();String output = "";try {for (int i = 0; i < input.length; i++) {if (java.lang.Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")) {String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);output += temp[0];} elseoutput += java.lang.Character.toString(input[i]);}} catch (BadHanyuPinyinOutputFormatCombination e) {log.error("中文转拼音出错",e);}return output;}/** * 获取汉字串拼音首字母,英文字符不变 * @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(); } }
使用
将字符串中的中文转为拼音(java)相关推荐
- java字符串去掉中文_Java——去除字符串中的中文
import java.util.regex.Matcher; import java.util.regex.Pattern; public class RemoveStrChinese { priv ...
- java将中文转为拼音
java将中文转为拼音 项目需求,需要将一批工号批量入库,但产品经理提供的却是中文--没办法,只好用程序转了. 做法是将人名保存为文本文件,每行一个.并且采用pinyin4j包进行转换,值得提的是pi ...
- 学点实用工作小技巧【Python】汉字转拼音、繁体字和简体字互转、提取字符串中的中文(英文)、判断是否纯中文(英文)
大家早上好,本人姓吴,如果觉得文章写得还行的话也可以叫我吴老师.欢迎大家跟我一起走进数据分析的世界,一起学习! 感兴趣的朋友可以关注我或者我的数据分析专栏,里面有许多优质的文章跟大家分享哦. 前言 又 ...
- 传入一个中文字符串,返回一个字符串中的中文拼音
/** * @param 传入一个中文字符串 * @return 返回一个字符串中的中文拼音 */ private String getNameNum(Strin ...
- Java通过正则剔除乱码_正则表达式 - 去掉乱码字符/提取字符串中的中文字符/提取字符串中的大小写字母 - Python代码...
目录 1.乱码符号种类较少,用replace() 2.乱码字符种类较多,用re.sub() 3.提取字符串中的中文字符 4.提取字符串中的中文字符和数字 5.提取其他 数据清洗的时候一大烦恼就是数据中 ...
- php怎么把中文转,PHP如何将中文转为拼音?
PHP如何将中文转为拼音? 首先使用Composer安装"overtrue/pinyin"拓展:composer require overtrue/pinyin 然后引入Pinyi ...
- php实现拼音转中文,PHP如何将中文转为拼音?
PHP如何将中文转为拼音? 首先使用Composer安装"overtrue/pinyin"拓展:composer require overtrue/pinyin 然后引入Pinyi ...
- 【Python】pinyin模块将中文转为拼音
[Python]pinyin模块将中文转为拼音 示例代码 示例代码 可以看到数据不是很规范,城市名称既有中文又有英文,而且上海被存储为ShangHai和Shanghai. 对于上海的问题,我们将拼音全 ...
- mysql得到中文的拼音_在MySQL中获取中文的拼音或转换中文替拼音
在MySQL中获取中文的拼音或转换中文为拼音 MySQL ------------------------------------- 分页:select * from xxx limit 0,10 ? ...
最新文章
- 基于javaGUI的文档识别工具制作
- 6.15 Unity引擎渲染效率全解析
- 子路由里嵌套子路由、嵌套多重子路由
- 计算机英语仲裁合词,仲裁 arbitration 法律英语常用词
- mybatis入门基础(五)----动态SQL
- 【hortonworks/registries】Parameter Schema name is null
- HAProxy安装与配置(一)
- 微信小程序上传视频功能的简单实现
- C++——次幂运算表示
- 【python】电商批量打标logo,超快速超简单!!!
- Ellisys Bluetooth Vanguard - 软件
- RedisTemplate that could not be found如何解决?
- lab值意义_lab是什么意思?
- 仿真(Simulation)
- 软件系统安全性测试列表(Checklist)
- 软件设计师-系统开发与软件工程
- 娱乐头条-03spider
- python翻译-python软件翻译
- cookie大小限制
- C#参数列表中的this(扩展方法)
热门文章
- 社区宽带繁忙是什么意思_十年宽带维修工程师给你解读,为什么宽带越用越慢!...
- Windows系统上的软件(如:爱奇艺万能播放器)固定任务栏图标后,打开后出现新任务栏图标
- qt5应用程序打包发布和qt5的mysql驱动编译
- Swift学习之--TableView的基本使用
- 分形造型的常用模型,C构造简单的IFS图形
- 三维地图(3D地图)离线地图开发
- 2. web前端开发分享-css,js进阶篇
- Hexo-Theme-Sakura 实践记录
- 【IVIF:特征聚合网络】
- WCF技术剖析之十四:泛型数据契约和集合数据契约(上篇)