利用Java开源库把汉字转拼音
来源:互联网 发布:三国志3优化版1.76 编辑:程序博客网 时间:2024/06/05 16:08
最近做的项目,因为下拉框中的项目太多,需要有个过滤的方法。我想的是键入汉语拼音首字母即时过滤下拉内容。
在网上找了好一会,发现了有个专门用来将中文转换成汉语拼音的Java的开源库,Pinyin4j,
下载地址:pinyin4j-2.5.0.zip
基本用法,如下示例
- /**
- * 汉字转换位汉语拼音,英文字符不变
- * @author xuke
- *
- */
- public class Cn2Spell {
- /**
- * 汉字转换位汉语拼音首字母,英文字符不变
- * @param chines 汉字
- * @return 拼音
- */
- public static String converterToFirstSpell(String chines){
- String pinyinName = "";
- char[] nameChar = chines.toCharArray();
- HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
- defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
- 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 chines 汉字
- * @return 拼音
- */
- public static String converterToSpell(String chines){
- String pinyinName = "";
- char[] nameChar = chines.toCharArray();
- HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
- defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
- 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];
- } catch (BadHanyuPinyinOutputFormatCombination e) {
- e.printStackTrace();
- }
- }else{
- pinyinName += nameChar[i];
- }
- }
- return pinyinName;
- }
- public static void main(String[] args) {
- System.out.println(converterToFirstSpell("欢迎来到最棒的Java中文社区"));
- }
- }
0 0
- 利用Java开源库把汉字转拼音
- Java把汉字转拼音
- java利用ascii码表,把汉字转成拼音(完整版)
- java利用ascii码表,把汉字转成拼音(完整版)
- java利用ascii码表,把汉字转成拼音(完整版)
- EXCEL利用VBA把汉字转拼音(李晓锋版)
- 利用 pinyin4j 把汉字转化为拼音
- 利用 pinyin4j 把汉字转化为拼音
- JAVA实现把汉字转化成拼音(转)
- java 汉字转拼音
- JAVA汉字转拼音
- java汉字转拼音
- java汉字转拼音
- java汉字转拼音
- Java 汉字转拼音
- 汉字转拼音java
- java汉字转拼音
- java 汉字转拼音
- Spring Web MVC配置
- RTX, uCOS-II, FreeRTOS embOS, uCOS-III的综合性能PK
- DOM解析XML
- 【D3.JS数据可视化实战记录】绘制力学图
- uploadify上传文件功能
- 利用Java开源库把汉字转拼音
- copyfile与movefile
- Codeforces Round #277.5 (Div. 2)C——Given Length and Sum of Digits...
- 常用jQuery数字类型的正则表达式
- [LeetCode] Pow(x, n)
- Android 中LayoutInflater的inflate函数用法详解
- C#高级编程(5) Array .Sort()、IComparable 、IComparer
- Tomcat中temp文件夹出现项目副本问题的解决方法
- Appium(Java)2.0.0以上运行时报错“java.lang.UnsupportedClassVersionError: io/appium/java_client/AppiumDriver”