1、正则表达式来判断字符串中是否包含字母
/**
* 使用正则表达式来判断字符串中是否包含字母
* @param str 待检验的字符串
* @return 返回是否包含
* true: 包含字母 ;false 不包含字母
*/
public static boolean judgeContainsStr(String str) {
String regex=".*[a-zA-Z]+.*";
Matcher m= Pattern.compile(regex).matcher(str);
return m.matches();
}
2、获得汉字首字母(通常用作通讯录检索)
public static String getAlpha(String chines) {
String pinyinName = "";
char[] nameChar = chines.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < nameChar.length; i++) {
System.out.println(nameChar[i]);
if (nameChar[i] > 128) {
try {
String[] ac = PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat);
if(ac!=null&&ac.length>0){
pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0].charAt(0);
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
return "#";
} catch (Exception e){
return "#";
}
} else {
if(judgeContainsStr(String.valueOf(nameChar[i]))){
pinyinName += nameChar[i];
}else{
pinyinName += "#";
}
}
}
return pinyinName;
}
3、获得汉语拼音首字母简称
public static String getAlphaDefaultCode(String chines) {
String pinyinName = "";
char[] nameChar = chines.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < nameChar.length; i++) {
if (nameChar[i] > 128) {
try {
String[] ac = PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat);
if(ac!=null&&ac.length>0){
pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0].charAt(0);
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
return "#";
} catch (Exception e){
return "#";
}
} else {
if(judgeContainsStr(String.valueOf(nameChar[i]))){
pinyinName += nameChar[i];
}else{
pinyinName += "#";
}
}
}
if(StringUtils.isBlank(pinyinName)){
pinyinName = "#";
}
return pinyinName;
}
4、将字符串中的中文转化为拼音,英文字符不变
/**
* 将字符串中的中文转化为拼音,英文字符不变
*
* @param inputString
* 汉字
* @return
*/
public static String getPingYin(String inputString) {
inputString = cleanChar(inputString);
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
format.setVCharType(HanyuPinyinVCharType.WITH_V);
String output = "";
if (inputString != null && inputString.length() > 0 && !"null".equals(inputString)) {
char[] input = inputString.trim().toCharArray();
try {
for (int i = 0; i < input.length; i++) {
if (Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")) {
String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);
output += temp[0];
} else
output += Character.toString(input[i]);
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
return "*";
}
return output;
}
5、汉字转换位汉语拼音首字母,英文字符不变
/**
* 汉字转换位汉语拼音首字母,英文字符不变
*
* @param chines
* 汉字
* @return 拼音
*/
public static String converterToFirstSpell(String chines) {
chines = cleanChar(chines);
String pinyinName = "";
char[] nameChar = chines.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < nameChar.length; i++) {
if (nameChar[i] > 128) {
try {
pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0].charAt(0);
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pinyinName += nameChar[i];
}
}
return pinyinName;
}
6、清理特殊字符
/**
* 清理特殊字符
* @param chines
* @return
*/
public static String cleanChar(String chines) {
chines = chines.replaceAll("[\\p{Punct}\\p{Space}]+", ""); // 正则去掉所有字符操作
// 正则表达式去掉所有中文的特殊符号
String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}<>《》【】‘;:”“’。,、?]";
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(chines);
chines = matcher.replaceAll("").trim();
return chines;
}
测试:
public static void main(String[] args) {
System.out.println("==获得汉字首字母:======="+getAlpha("测试"));
System.out.println("==获得汉语拼音首字母:======="+getAlphaDefaultCode("测试一下"));
System.out.println("==中文转化为拼音,英文字符不变:======="+getPingYin("测试一下test"));
System.out.println("==汉语拼音首字母,英文字符不变:======="+converterToFirstSpell("测试一下test"));
System.out.println("==清理特殊字符:======="+cleanChar("测试一下2!@#¥%……SDmm"));
}
}
结果:
测
试
==获得汉字首字母:=======CS
==获得汉语拼音首字母:=======CSYX
==中文转化为拼音,英文字符不变:=======ceshiyixiatest
==汉语拼音首字母,英文字符不变:=======CSYXtest
==清理特殊字符:=======测试一下2SDmm
Process finished with exit code 0