Java 中文转换拼音工具
Java 中文转换拼音工具
/**
* <html>
* <body>
* <P> Copyright 1994 JsonInternational</p>
* <p> All rights reserved.</p>
* <p> Created on 19941115</p>
* <p> Created by Jason</p>
* </body>
* </html>
*/
package cn.ucaner.alpaca.framework.utils.chinese; 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.exception.BadHanyuPinyinOutputFormatCombination; /**
* @Package:cn.ucaner.framework.utils
* @ClassName:PinyinUtils
* @Description: <p> 中文转换拼音工具</p>
* @Author: -
* @CreatTime:2017年8月30日 下午2:09:07
* @Modify By:
* @ModifyTime:
* @Modify marker:
* @version V1.0
*/
public class PinyinUtils { /**
* 获取汉字串拼音首字母,英文字符不变
* @param chinese 汉字串
* @return 汉语拼音首字母
*/
public static String getChineseFirstWord(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[] word = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
if (word != null) {
pybf.append(word[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 getChineseAllWord(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();
} /**
* For Test By Jason
*/
public static void main(String[] args) {
//issue: 不能空格 woaizhongguo! byJson
System.out.println(getChineseAllWord("我爱中国! byJson"));
System.out.println(getChineseFirstWord("我爱中国! byJson"));//wazgbyJson
}
}
Java 中文转换拼音工具的更多相关文章
- NPinyin 中文转换拼音代码
Mono 3.2 测试NPinyin 中文转换拼音代码 C#中文转换为拼音NPinyin代码 在Mono 3.2下运行正常,Spacebuilder 有使用到NPinyin组件,代码兼容性没有问 ...
- java 中文转拼音之pinyin4j
一.简介 有时候,须要将汉字编程相应的拼音.以方便数据的处理.比方在Android手机应用的开发上.要查询联系人的姓名.通常都是用拼音进行查询的. 比方要查询"曹孟德",就能够输入 ...
- Mono 3.2 测试NPinyin 中文转换拼音代码
C#中文转换为拼音NPinyin代码 在Mono 3.2下运行正常,Spacebuilder 有使用到NPinyin组件,代码兼容性没有问题. using System; using System. ...
- 在QML 中用javascritpt 将中文转换拼音,可以在音标
项目需要, 今天整理了一下.在QML调用javascrit将中文汉字转换成拼音. 感觉执行效率低.下面是主要代码. 具体代码请参考QMLPinyin 代码 ```import "./piny ...
- java 中文转换成Unicode编码和Unicode编码转换成中文
转自:一叶飘舟 http://blog.csdn.net/jdsjlzx/article/details/ package lia.meetlucene; import java.io.IOExcep ...
- PHP中文转拼音扩展
Pinyin 基于 CC-CEDICT 词典的中文转拼音工具,更准确的支持多音字的汉字转拼音解决方案. 安装 使用 Composer 安装: $ composer require "over ...
- C#中文转换为拼音NPinyin代码【转】
项目地址:https://code.google.com/p/npinyin/ 在一个采集的程序中,为了给每个文章起一个别名,据说有很好的别名的话,对于百度.google的收录 是很有好处的.按照Se ...
- java util - 中文、繁体转成拼音工具pinyin4j
需要 pinyin4j-2.5.0.jar 包 代码例子 package cn.java.pinyin4j; import net.sourceforge.pinyin4j.PinyinHelper; ...
- java PinYinUtils 拼音工具类
package com.sicdt.library.core.utils; import java.util.HashSet; import java.util.Set; import net.sou ...
随机推荐
- Pytorch中ndarray tensor list互转
1.ndarray->tensor : b=torch.from_numpy(a) 2.tensor->ndarray: b=a.numpy() ''' 但这么写会报错-- Runtime ...
- leetcode 877. 石子游戏
题目描述: 亚历克斯和李用几堆石子在做游戏.偶数堆石子排成一行,每堆都有正整数颗石子 piles[i] . 游戏以谁手中的石子最多来决出胜负.石子的总数是奇数,所以没有平局. 亚历克斯和李轮流进行,亚 ...
- 【重庆师范大学】PHP博客训练-Thinkphp
设计数据库 CREATE TABLE `user` ( `user_id` int unsigned NOT NULL AUTO_INCREMENT, `username` varchar() COM ...
- vmware vsphere各版本差别,及各套件差别
最近要开始全面支持虚拟化了,客户私有云环境用的多的为vmware vsphere,特地恶补下vmware vsphere的各个差别. 首先是vSphere,ESXi和vCenter 的区别. ESXi ...
- Python适合练手的项目
原文地址:https://www.jianshu.com/p/039156321e30 项目地址:https://github.com/DeqianBai/Python-Project/tree/ma ...
- easyui datagrid的editor编辑器如何为validatebox控件添加改变事件
项目中需要为行编辑器Editor的某个列的文本框添加改变事件 需求:新增行时,为用户名输入特殊字符进行验证,不允许保存用户数据 html页面 <table id="gridlist&q ...
- jetty源码下载
jetty下载地址:https://www.eclipse.org/jetty/download.html Release 9.4.20.v20190813 .zip .tgz api ...
- 重装系统之前需要做的checklist
1. 各浏览器 ---- 导出收藏夹 2. 备份桌面 3. 查用工具截图保存.保存使用了哪些工具 4.查看C盘有没有放置其他资料,需要备份的
- RedHat 7关闭防火墙方法
1.在之前的版本中关闭防火墙等服务的命令是 service iptables stop /etc/init.d/iptables stop 2.RHEL7开始,使用systemctl工具来管理服务程序 ...
- Request header field * is not allowed by Access-Control-Allow-Headers in preflight response问题解决
跨域问题报错信息为:Failed to load http://192.168.30.119: Request header field language is not allowed by Acce ...