javaSE_中文转拼音
使用第三方包:pinyin4j-2.5.0.jar
/**
* 汉字转拼音,非汉字保留
* @param hanziString 汉字串
* @param toneType 音调类型
* @return 拼音串
*/
public static String getPinyinAll(String hanziString, HanyuPinyinToneType toneType){
if(hanziString != null && !hanziString.trim().equalsIgnoreCase("")){
char[] srcChar = hanziString.trim().toCharArray();
HanyuPinyinOutputFormat hanYuPinOutputFormat = new HanyuPinyinOutputFormat();
hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); // 小写
hanYuPinOutputFormat.setToneType(toneType); // 音调
hanYuPinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE); // 编码
StringBuffer output = new StringBuffer();
for(int i=0;i<srcChar.length;i++){
char c = srcChar[i];
//if(String.valueOf(c).matches("[//u4E00-//u9FA5]+")){ //中文字符
if(isChineseChar(c)){ //中文字符
try{
String[] temp = PinyinHelper.toHanyuPinyinStringArray(srcChar[i],hanYuPinOutputFormat);
output.append(temp[0]);
output.append(" ");
}catch(BadHanyuPinyinOutputFormatCombination e){
e.printStackTrace();
}
}else{ //其他字符
output.append(String.valueOf(srcChar[i]));
}
}
return output.toString();
}
return null;
}
/**
* 汉字转拼音并取首字母,非汉字保留
* @param hanziString 汉字串
* @return 拼音首字母串
*/
public static String getPinyinHead(String hanziString){
if(hanziString != null && !hanziString.trim().equalsIgnoreCase("")){
char[] srcChar = hanziString.trim().toCharArray();
HanyuPinyinOutputFormat hanYuPinOutputFormat = new HanyuPinyinOutputFormat();
hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); // 小写
hanYuPinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); // 音调
hanYuPinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE); // 编码
StringBuffer output = new StringBuffer();
for(int i=0;i<srcChar.length;i++){
char c = srcChar[i];
if(isChineseChar(c)){ //中文字符
try{
String[] temp = PinyinHelper.toHanyuPinyinStringArray(srcChar[i],hanYuPinOutputFormat);
output.append(temp[0].charAt(0));
}catch(BadHanyuPinyinOutputFormatCombination e){
e.printStackTrace();
}
}else{ //其他字符
output.append(String.valueOf(srcChar[i]));
}
}
return output.toString();
}
return null;
}
// 判断一个字符是否为中文
public static boolean isChineseChar(char ch){
try {
return String.valueOf(ch).getBytes("GBK").length > 1;
} catch (Exception e) {
}
return false;
}
使用:
static public void main(String[] args){
String str = "我是中国人! I'm Chinese!";
System.out.println(getPinyinAll(str, HanyuPinyinToneType.WITH_TONE_MARK));// 使用音调
System.out.println(getPinyinAll(str, HanyuPinyinToneType.WITH_TONE_NUMBER));// 音调使用数字
System.out.println(getPinyinAll(str, HanyuPinyinToneType.WITHOUT_TONE));// 不用音调
System.out.println(getPinyinHead(str));// 获取拼音首字母
}
输出:
wŏ shì zhōng guó rén ! I'm Chinese!
wo3 shi4 zhong1 guo2 ren2 ! I'm Chinese!
wo shi zhong guo ren ! I'm Chinese!
wszgr! I'm Chinese!
javaSE_中文转拼音相关推荐
- Elasticsearch1.x 基于lc-pinyin和ik分词实现 中文、拼音、同义词搜索
一.简介 有时候我们需要在项目中支持中文 和 拼音的搜索.采用ik分词来做中文分词是目前比好的方式.至于拼音分词可以采用lc-pinyin,虽然lc-pinyin能很好的解决首字母和全拼的搜索,但是在 ...
- 中文按拼音首字母排序的C++实现方案
先介绍下背景,最近有个需求,需要将用户的好友列表按照昵称的拼音首字母排序,类似于手机电话簿的联系人.一开始建议让终端同学去做这点,毕竟终端现有的电话簿模块已经实现了,不过终端同学说电话簿是系统组件,移 ...
- PHP中文获取拼音函数
<?php //中文获取拼音函数 /* return String $_String 输入的中文字符 $_Code 输出字符编码类型,默认是UTF-8 */ function pinyin($_ ...
- mysql怎么创建文字表_myMySQL数据库 如何将中文转拼音 MySQL数据库使用教程
mysql将中文转拼音的方法:首先创建一个数据库,并创建一张汉字和拼音对照表:然后使用"INSERT INTO t_base_pinyin () VALUES ()"语句来给汉字拼 ...
- php 中文拼音,php中文转拼音
使用环境 thinkphp3.2.3 基于composer安装 使用过程 tip: "overtrue/pinyin": "~3.0" 主要添加了这段 { &q ...
- python缩写转换成全拼_Python中文转拼音代码(支持全拼和首字母缩写)
#!/usr/bin/env python # -*- coding:utf-8 -*- """ 原版代码:https://github.com/cleverdeng/p ...
- symbian获取中文的拼音
//--------------------------------------------------------------获取短字符8位的 汉字拼音--------------------- ...
- 中文转拼音 中文转罗马声调
中文转拼音 和 声调,支持多音字 字典查询模式,本地可维护和扩展字典 感兴趣的可以直接下载源码: https://download.csdn.net/download/snowwind_51/2353 ...
- php怎么把中文转,PHP如何将中文转为拼音?
PHP如何将中文转为拼音? 首先使用Composer安装"overtrue/pinyin"拓展:composer require overtrue/pinyin 然后引入Pinyi ...
最新文章
- 线刷一加5t android 9,一加5/5T 氢OS 9.0稳定四版 侧边工具 通知特效 Magisk 极速流畅 简约实用-刷机之家...
- w3m linux,Linux 终端浏览器 w3m
- 工作流中切换数据库时---“禁止流转”
- 蓝桥杯2017初赛-打印大X-找规律
- 计算机桌面文件夹排序,电脑桌面文件整理前后对照
- UVa 1588 换抵挡装置
- Centos/Linux下如何查看网关地址/Gateway地址
- 关于redis的pipline和lua脚本
- python chardet_Python - chardet
- python aiml_Python AIML搭建聊天机器人实例
- 设计模式面对面之单例模式
- webgate 重构 工作进度计划
- U8系统UFO报表无法打印
- atomic 原子量的使用心得
- CentOS7安装Zeppelin完整步骤
- C++学习(11)(综合题)
- 2021最新微信影视小程序源码无限代开+搭建详细教程
- 特征值(特征向量)与相似对角化
- 大学生数学建模优秀论文发表
- 分享几个对话配音使用的软件给你