汉子与拼音转化类

/**
 * 
 */
package com.iflytek.atp.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

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;

/**
 * @author admin
 *
 */
public class StringUtils {
	/**
	 * 
	 * @Title: getPingYin   
	 * @Description: (将汉字转换为全拼音)   
	 * @param: @param src
	 * @param: @return      
	 * @return: String 
	 * @author: yh
	 * @date:   2018年5月4日 下午4:39:05  
	 * @throws
	 */
	public static String getPingYin(String src) {
		char[] t1 = null;
		t1 = src.toCharArray();
		String[] t2 = new String[t1.length];
		HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();

		t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		t3.setVCharType(HanyuPinyinVCharType.WITH_V);
		String t4 = "";
		int t0 = t1.length;
		try {
			for (int i = 0; i < t0; i++) {
				if (java.lang.Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
					t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
					t4 += t2[0];
				} else{
					t4 += java.lang.Character.toString(t1[i]);
				}
			}
			// System.out.println(t4);
			return t4;
		} catch (BadHanyuPinyinOutputFormatCombination e1) {
			e1.printStackTrace();
		}
		return t4;
	}

	/**
	 * 
	 * @Title: getPinYinHeadChar   
	 * @Description: (获取字符串首字母)   
	 * @param: @param str
	 * @param: @return      
	 * @return: String 
	 * @author: yh
	 * @date:   2018年5月4日 下午4:39:39  
	 * @throws
	 */
	public static String getPinYinHeadChar(String str) {

		String convert = "";
		for (int j = 0; j < str.length(); j++) {
			char word = str.charAt(j);
			String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
			if (pinyinArray != null) {
				convert += pinyinArray[0].charAt(0);
			} else {
				convert += word;
			}
		}
		return convert;
	}

	/**
	 * 
	 * @Title: getCnASCII   
	 * @Description: (获取汉子ASCII码)   
	 * @param: @param cnStr
	 * @param: @return      
	 * @return: String 
	 * @author: yh
	 * @date:   2018年5月4日 下午4:40:13  
	 * @throws
	 */
	public static String getCnASCII(String cnStr) {
		StringBuffer strBuf = new StringBuffer();
		byte[] bGBK = cnStr.getBytes();
		for (int i = 0; i < bGBK.length; i++) {
			strBuf.append(Integer.toHexString(bGBK[i] & 0xff));
		}
		return strBuf.toString();
	}
	/**
	 * 
	 * @Methodname:StringFilter(过滤特殊字符)
	 * @Description:(这里描述这个方法适用条件 – 可选)
	 * @Param @param str
	 * @Param @return
	 * @Param @throws PatternSyntaxException 
	 * @Return String
	 * @Author emtf
	 * @Datetime 2018年9月15日 下午4:49:58
	 * @Exception 
	 * @Since  1.0.0
	 */
	public static String StringFilter(String str) throws PatternSyntaxException { 
		String regEx="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]"; 
		Pattern p = Pattern.compile(regEx); 
		Matcher m = p.matcher(str);
		return m.replaceAll("").trim();
	} 
}

猜你喜欢

转载自blog.csdn.net/chinasi2012/article/details/86062712
今日推荐