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;
@SuppressWarnings("restriction")
public class SpellHelper {
public SpellHelper(){
super();
}
//将中文转换为英文
@SuppressWarnings({ "deprecation" })
public static String getEname(String name) throws Exception{
HanyuPinyinOutputFormat pyFormat = new HanyuPinyinOutputFormat();
pyFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
pyFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
pyFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
return PinyinHelper.toHanyuPinyinString(name, pyFormat, "");
}
//将中文转换为英文
@SuppressWarnings({ "deprecation" })
public static String getbeginname(String name) throws Exception{
HanyuPinyinOutputFormat pyFormat = new HanyuPinyinOutputFormat();
pyFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
pyFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
pyFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
return PinyinHelper.toHanyuPinyinString(name, pyFormat, "");
}
//姓、名的第一个字母需要为大写
public static String getUpEname(String name) {
char[] strs = name.toCharArray();
String newname = null;
try {
//名字的长度 www.2cto.com
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])) + ""
+toUpCase(getEname("" + strs[1])) + ""
+toUpCase(getEname("" + strs[2])) + ""
+toUpCase(getEname("" + strs[3]));
} else {
StringBuffer sb=new StringBuffer();
for(int i=0;i<strs.length;i++){
sb.append(toUpCase(getEname("" + strs[i])));
}
newname =sb.toString();//toUpCase(getEname(name));
}
} catch (Exception e) {
e.printStackTrace();
}
return newname;
}
//姓、名的第一个字母需要为大写
public static String getSpaname(String name) {
char[] strs = name.toCharArray();
String newname = null;
try {
//名字的长度 www.2cto.com
if (strs.length == 2) {
newname = strs[0] + " "
+ strs[1];
} else if (strs.length == 3) {
newname =strs[0] + " "
+ strs[1]+ " "
+ strs[2];
} else if (strs.length == 4) {
newname = name;
} else {
StringBuffer sb=new StringBuffer();
for(int i=0;i<strs.length;i++){
sb.append(strs[i]);
}
newname =sb.toString();//toUpCase(getEname(name));
}
} catch (Exception e) {
e.printStackTrace();
}
return newname;
}
//首字母大写
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();
}
//获取首字母
public static String getBeginEname(String name) {
char[] strs = name.toCharArray();
String newname = "";
try {
//名字的长度 www.2cto.com
for(int i=0;i<strs.length;i++){
String newstr=getbeginname(""+strs[i]);
newname=newname+newstr.substring(0, 1);
}
} catch (Exception e) {
e.printStackTrace();
}
return newname;
}
public static void main(String[] args) throws Exception {
System.out.println(getBeginEname("漯河市衢州市亳州市濮阳市儋州市"));
System.out.println(getEname("漯河市衢州市亳州市濮阳市儋州市"));
}
}