拼音转换
来源:互联网 发布:淘宝店铺微淘入口 编辑:程序博客网 时间:2024/03/29 06:30
一个强大的中文转换拼音的开源组件。package com.unutrip.remoting.ws;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;/** * * pinyin4j使用 * * @author longgangbai * */public class CNSpell { public static HanyuPinyinToneType[] toneTypes = new HanyuPinyinToneType[] { HanyuPinyinToneType.WITH_TONE_NUMBER, HanyuPinyinToneType.WITHOUT_TONE, HanyuPinyinToneType.WITH_TONE_MARK }; public static HanyuPinyinVCharType[] vCharTypes = new HanyuPinyinVCharType[] { HanyuPinyinVCharType.WITH_U_AND_COLON, HanyuPinyinVCharType.WITH_V, HanyuPinyinVCharType.WITH_U_UNICODE }; public static HanyuPinyinCaseType[] caseTypes = new HanyuPinyinCaseType[] { HanyuPinyinCaseType.LOWERCASE, HanyuPinyinCaseType.UPPERCASE }; /** * 将中文字符转换为相应的数组 * * @param chineseCharacter * @param toneSelection * @param vcharSelection * @param caseSelection */ private static void updateFormattedText(String chineseCharacter, HanyuPinyinToneType toneSelection, HanyuPinyinVCharType vcharSelection, HanyuPinyinCaseType caseSelection) { // 拼音格式化对象 HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat(); // 设置中文声调 if (toneSelection != null) { if (toneTypes[0] == toneSelection) { outputFormat.setToneType(HanyuPinyinToneType.WITH_TONE_NUMBER); } else if (toneTypes[1] == toneSelection) { outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); } else if (toneTypes[2] == toneSelection) { outputFormat.setToneType(HanyuPinyinToneType.WITH_TONE_MARK); } } // 设置特殊拼音的转换 if (vcharSelection != null) { if (vCharTypes[0] == vcharSelection) { outputFormat .setVCharType(HanyuPinyinVCharType.WITH_U_AND_COLON); } else if (vCharTypes[1] == vcharSelection) { outputFormat.setVCharType(HanyuPinyinVCharType.WITH_V); } else if (vCharTypes[2] == vcharSelection) { outputFormat.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE); } } // 设置转换的拼音的大小写 if (caseSelection != null) { if (caseTypes[0] == caseSelection) { outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); } else if (caseTypes[1] == caseSelection) { outputFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE); } } // 获取字符串 char[] cnStr = chineseCharacter.toCharArray(); for (char chineseChar : cnStr) { // 将字符转换为拼音数组 String[] pinyinArray = null; try { pinyinArray = PinyinHelper.toHanyuPinyinStringArray( chineseChar, outputFormat); } catch (BadHanyuPinyinOutputFormatCombination e1) { e1.printStackTrace(); } String outputString = concatPinyinStringArray(pinyinArray); System.out.println(outputString); } } /** * 测试中文转换拼音方法 * * @param args */ public static void main(String[] args) { updateFormattedText("我是中国人!", HanyuPinyinToneType.WITH_TONE_MARK, HanyuPinyinVCharType.WITH_U_UNICODE, HanyuPinyinCaseType.UPPERCASE); } /** * 将转换的拼音数组转换为字符串 * * @param pinyinArray * @return */ private static String concatPinyinStringArray(String[] pinyinArray) { StringBuffer pinyinStrBuf = new StringBuffer(); if ((null != pinyinArray) && (pinyinArray.length > 0)) { for (int i = 0; i < pinyinArray.length; i++) { pinyinStrBuf.append(pinyinArray[i]); pinyinStrBuf.append(System.getProperty("line.separator")); } } String outputString = pinyinStrBuf.toString(); return outputString; }}
- 拼音转换
- 拼音转换
- 汉字转换成拼音
- 汉字转换成拼音
- 中文转换拼音程序
- 汉字转换拼音
- SQL汉字转换拼音
- 汉字转换拼音
- Symbian 汉字拼音转换
- PHP拼音转换
- 汉字转换成拼音
- 汉字转换成拼音
- 中文名转换成拼音
- php 汉语转换拼音
- 汉字转换成拼音
- 汉字转换成拼音
- 汉字转换为拼音
- java汉字转换拼音
- Struts2上传下载(转)
- Hibernate3 struts2 spring2 clob操作
- Struts2上传下载(转)(二)
- Struts1与Struts2原理 区别 详解 汇总
- Struts2 blob(转)
- 拼音转换
- 验证码
- 分页程序(非通用版)简单版
- Struts1 上传二进制流
- MFC设备描述表
- SSH高效分页
- FCKeditor文本编辑器
- My97DataPicker
- 办公管理系统(当初学的时候做练习的)jsp+servlet+javabean