package com.zq.utils.string;
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;
/**
* 汉字转拼音工具类 Created by MyEclipse. Author: ChenBin E-mail: chenbin_2008@126.com
* Date: 2015-1-23 Time: 上午10:46:24 Company: 湖南八万信息科技有限公司 Web sites:
* http://8000056.com Version: 5.0 依赖包:pinyin4j.jar
*/
public class SpellUtils {
/**
* @author : ChenBin
* @date : 2015-3-9 上午11:06:52
* @Description :将中文转换为拼音
*/
public static String getEname(String name) {
HanyuPinyinOutputFormat pyFormat = new HanyuPinyinOutputFormat();
pyFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
pyFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
pyFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
return PinyinHelper.toHanyuPinyinString(name, pyFormat, "");
}
/**
* @author : ChenBin
* @date : 2015-3-9 上午11:07:06
* @Description : 姓、名的第一个字母需要为大写
*/
public static String getUpEname(String name) {
char[] strs = name.toCharArray();
String newname = null;
// 名字的长度
if (strs.length == 2)
newname = toUpCase(getEname("" + strs[0])) + " " + toUpCase(getEname("" + strs[1]));
else if (strs.length == 3)
newname = toUpCase(getEname("" + strs[0])) + " " + toUpCase(getEname("" + strs[1] + strs[2]));
else if (strs.length == 4)
newname = toUpCase(getEname("" + strs[0] + strs[1])) + " " + toUpCase(getEname("" + strs[2] + strs[3]));
else
newname = toUpCase(getEname(name));
return newname;
}
/**
* @author : ChenBin
* @date : 2015-3-9 上午11:07:19
* @Description :首字母大写
*/
private static String toUpCase(String str) {
StringBuffer newstr = new StringBuffer();
newstr.append((str.substring(0, 1)).toUpperCase()).append(str.substring(1, str.length()));
return newstr.toString();
}
/**
* @author : ChenBin
* @date : 2015-3-9 上午11:07:32
* @Description :获取汉字串拼音首字母,英文字符不变,汉语拼音首字母
* @param str
* 汉字串
*/
public static String getFirstSpell(String str) {
if (!StringUtils.compareTrim(str))
return "";
StringBuffer pybf = new StringBuffer();
char[] arr = str.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) {
String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
if (temp != null) {
pybf.append(temp[0].charAt(0));
}
} else {
pybf.append(arr[i]);
}
}
return pybf.toString().replaceAll("\W", "").trim();
}
/**
* @author : ChenBin
* @date : 2015-3-9 上午11:08:21
* @Description :获取汉字串拼音,英文字符不变,汉语拼音
* @param str
* 汉字串
*/
public static String getFullSpell(String str) {
StringBuffer pybf = new StringBuffer();
char[] arr = str.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) {
pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]);
} else {
pybf.append(arr[i]);
}
}
return pybf.toString();
}
public static void main(String[] args) {
String str = "李兵";
System.out.println(StringUtils.upperCase(getFirstSpell(str)));// 获得首字母
System.out.println(StringUtils.upperCase(getFullSpell(str)));// 全拼
System.out.println(getUpEname("欧阳震华"));// 名字全拼,大写开头字母并分隔姓、名
}
}