终结者:借助pinyin4j相关jar包提取汉字的首字母
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 Test { /**
* 提取汉字的首字母。假设里面含有费中文字符则忽略之;假设全为非中文则返回""。
* @param caseType 当为1时获取的首字母为小写,否则为大写。
* @author 高焕杰
*/
public static String getPinYinHeadChar(String zn_str, int caseType) {
if(zn_str != null && !zn_str.trim().equalsIgnoreCase("")) {
char[] strChar = zn_str.toCharArray();
// 汉语拼音格式输出类
HanyuPinyinOutputFormat hanYuPinOutputFormat = new HanyuPinyinOutputFormat();
// 输出设置。大写和小写,音标方式等
if(1 == caseType) {
hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
} else {
hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
}
hanYuPinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
hanYuPinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
StringBuffer pyStringBuffer = new StringBuffer();
for(int i=0; i<strChar.length; i++) {
char c = strChar[i];
char pyc = strChar[i];
if(String.valueOf(c).matches("[\\u4E00-\\u9FA5]+")) {//是中文或者a-z或者A-Z转换拼音
try {
String[] pyStirngArray = PinyinHelper.toHanyuPinyinStringArray(strChar[i], hanYuPinOutputFormat);
if(null != pyStirngArray && pyStirngArray[0]!=null) {
pyc = pyStirngArray[0].charAt(0);
pyStringBuffer.append(pyc);
}
} catch(BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
}
}
return pyStringBuffer.toString();
}
return null;
} public static void main(String[] args) {
String cnStr = "刘菲菲";
System.out.println(getPinYinHeadChar(cnStr, 1)); //输出lff
System.out.println(getPinYinHeadChar(cnStr, 2)); //输出LFF
}
}
终结者:借助pinyin4j相关jar包提取汉字的首字母的更多相关文章
- (转载)C#提取汉字拼音首字母的方法
今天突然要用到提取汉字拼音首字母的功能,去网上找了找,发现没有几个好用的,决定自己写一个,效果还不错,发出来大家一起研究下,分享给大家!直接入主题: 1.首先对编码进行定义 #region 编码定义 ...
- C# 使用微软的Visual Studio International Pack 类库提取汉字拼音首字母
首先下载Visual Studio International Pack 1.0,官方下载地址:http://www.microsoft.com/downloads/zh-cn/details.asp ...
- C# 汉字转拼音 使用微软的Visual Studio International Pack 类库提取汉字拼音首字母
代码参考该文http://www.cnblogs.com/yazdao/archive/2011/06/04/2072488.html VS2015版本 1.使用Nuget 安装 "Simp ...
- excel提取汉字拼音首字母
本文转载:http://jingyan.baidu.com/article/63acb44adca44461fcc17e85.html 利用Excel表格中的宏,轻松提取首字母 方法/步骤 1.启动E ...
- Java 解析epub格式电子书,helloWorld程序,附带源程序和相关jar包
秀才坤坤出品 一.epub格式电子书 相关材料和源码均在链接中可以下载:http://pan.baidu.com/s/1bnm8YXT 包括 1.JAVA项目工程test_epub,里面包括了jar包 ...
- Spring4相关jar包介绍(转)
Spring4相关jar包介绍 spring-core.jar(必须):这个jar 文件包含Spring 框架基本的核心工具类.Spring 其它组件要都要使用到这个包里的类,是其它组件的基本核心,当 ...
- Spring3.2.2中相关Jar包的作用
今天在看Spring的源码的时候不知道从什么地方开启应该合适,因为不太清楚实现类所在的具体Jar包,就从网上找了些,可是网上有的说的是不清不楚,甚至是有些错误的,所以就把相关Jar包的大致作用给整理了 ...
- AutoCompleteTextView输入汉字拼音首字母实现过滤提示(支持多音字,Filterable的使用)
AutoCompleteTextView具有输入提示的功能,但是它的这种提示不适合对股票列表的过滤,如果你玩过股票软件,就会知道只要输入股票名称的首字母或股票代码就会出现符合匹配的股票,这种过滤怎么实 ...
- java获取汉字拼音首字母 --转载
在项目中要更能根据某些查询条件(比如姓名)的首字母作为条件进行查询,比如查一个叫"李晓明"的人,可以输入'lxm'.写了一个工具类如下: import java.io.Unsupp ...
随机推荐
- Python 上下文(Context)学习笔记
前言 最早接触到with语句的时候,是初学python,对文件进行读写的时候,当时文件读写一般都是用open()函数来对文件进行读写,为了防止读写的过程中出现错误,也为了让代码更加的pythonic, ...
- python Web抓取(二)selenium模块的使用、对浏览器的按键操作及错误处理
建议以下帖子: 教你在Windows上搭建Python+Selenium环境:https://blog.csdn.net/huilan_same/article/details/52888262 py ...
- Linux Virtual Server技术
1 LVS简单介绍 Linux VirtualServer是一个高扩展和高可用性server,在一个真正server的集群中构建而成,包括Linux操作系统中的负载均衡. server的架构对于终端用 ...
- Use Uncertainty As a Driver
 Use Uncertainty As a Driver Kevlin Henney ConFRonTEd WiTH TWo opTionS, most people think that the ...
- Using a Plugin
创建hello.world <?xml version="1.0"?> <sdf version="1.4"> <world na ...
- PipeCAD之管道标准库PipeStd(2)
PipeCAD之管道标准库PipeStd(2) eryar@163.com Key Words. PipeCAD, PipeStd, 管道设计软件,管件库 1. Introduction 管道标准部件 ...
- WordPress改动新用户注冊邮件内容--自己定义插件
有些开放用户注冊功能的WordPress站点,可能有这么一项需求,就是用户注冊成功后,系统会分别给站点管理员和新用户发送一封通知邮件.给管理员发送的是新用户的username和Email,给刚刚注冊的 ...
- vue22 路由
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- BZOJ2895: 球队预算
[传送门:BZOJ2895] 简要题意: 在一个篮球联赛里,有n支球队,球队的支出是和他们的胜负场次有关系的,具体来说,第i支球队的赛季总支出是Ci*x^2+Di*y^2,Di<=Ci.(赢得多 ...
- 初学Larevel 2014-08-21 11:24 90人阅读 评论(0) 收藏
添加第一个路由时就遇到了 404错误,查了一下说要这样才能 版权声明:本文为博主原创文章,未经博主允许不得转载.