汉字转拼音
来源:互联网 发布:mysql function判断 编辑:程序博客网 时间:2024/05/22 03:09
package com.wisemen.hhb.module.admin.util;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.util.StringUtils;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;public class PingYinUtil {private static final Logger logger = LoggerFactory.getLogger(PingYinUtil.class);/** * 将字符串中的中文转化为拼音,其他字符不变 * * @param inputString * @return */public static String getPingYin(String inputString) {HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();format.setCaseType(HanyuPinyinCaseType.LOWERCASE);format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);format.setVCharType(HanyuPinyinVCharType.WITH_V);char[] input = inputString.trim().toCharArray();String output = "";try {for (int i = 0; i < input.length; i++) {if (java.lang.Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")) {String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);output += temp[0];} elseoutput += java.lang.Character.toString(input[i]);}} catch (Exception e) {//无论遇到任何异常都不停止转换,而是忽略转换不了的字符logger.warn(e.getMessage());}return output;}/** * 获取汉字串拼音首字母,英文字符不变 * * @param chinese * 汉字串 * @return 汉语拼音首字母 */public static String getFirstSpell(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[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);if (temp != null) {pybf.append(temp[0].charAt(0));}} catch (Exception e) {//无论遇到任何异常都不停止转换,而是忽略转换不了的字符logger.warn("待转换的字符串:'" + chinese +"',包含无法转换的字符:'" + arr[i]+"'");}} else {pybf.append(arr[i]);}}return pybf.toString().replaceAll("\\W", "").trim();}/** * 获取汉字串拼音,英文字符不变 * * @param chinese * 汉字串 * @return 汉语拼音 */public static String getFullSpell(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 (Exception e) {//无论遇到任何异常都不停止转换,而是忽略转换不了的字符logger.warn("待转换的字符串:'" + chinese +"',包含无法转换的字符:'" + arr[i]+"'");}} else {pybf.append(arr[i]);}}return pybf.toString();}/** * 获取汉字串拼音,英文字符不变 ,每个拼音首字母大写 * * @param chinese * 汉字串 * @return 汉语拼音 */public static String getFullSpellFirstCharUp(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 temp = PinyinHelper.toHanyuPinyinStringArray(arr[i],defaultFormat)[0];pybf.append( firstLetterUp(temp) );} catch (Exception e) {//无论遇到任何异常都不停止转换,而是忽略转换不了的字符logger.warn("待转换的字符串:'" + chinese +"',包含无法转换的字符:'" + arr[i]+"'");}} else {pybf.append(arr[i]);}}return pybf.toString();}/** * * <p>Description:首字母大写</p> * <p>Paramenter:</p> * <p>Return:</p> * <p>Exception:</p> * <p>Modified History: </p> */private static String firstLetterUp( String str ){if( StringUtils.isEmpty(str) ){return "";}String firstLetter = str.substring(0,1);if( str.length() == 1 ){return firstLetter;}return firstLetter.toUpperCase() + str.substring(1, str.length());}public static void main(String[] args) {String testChinese = "汉子测试▪看看 有什么";System.out.println( getFullSpellFirstCharUp(testChinese) );}}
0 0
- 汉字转拼音
- 汉字转拼音缩写
- 汉字转拼音缩写
- 实用汉字转拼音
- 汉字转拼音
- 汉字转拼音 asp
- 汉字转拼音缩写
- 汉字转拼音
- 汉字转拼音缩写
- 汉字转拼音
- PHP汉字转拼音
- PHP汉字转拼音
- php汉字转拼音
- java 汉字转拼音
- 汉字转拼音(C#)
- VBS 汉字转拼音
- 汉字转拼音
- 汉字转拼音
- C++中的const引用和非const引用
- 虚函数
- Liferay开发学习(1)
- CSAPP 六个重要实验 lab1
- 产品想快速切入市场,就这6条途径
- 汉字转拼音
- 【转】【Coursera公开课】Applying to U.S. Universities(如何申请美国大学)笔记
- ajax登陆验证
- C++中STL对string进行trim,split,replace操作 (2012-05-25 09:56:12)
- Container With Most Water
- <从你的全世界路过>(张嘉佳)
- The 2014 ACM-ICPC Asia Mudanjiang Regional First Round
- java线程池的实现原理
- 密码格式验证,身份证验证