1、正則表達(dá)式來(lái)判斷字符串中是否包含字母
/**
* 使用正則表達(dá)式來(lái)判斷字符串中是否包含字母
* @param str 待檢驗(yàn)的字符串
* @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萍聊、獲得漢語(yǔ)拼音首字母簡(jiǎn)稱
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、將字符串中的中文轉(zhuǎn)化為拼音,英文字符不變
/**
* 將字符串中的中文轉(zhuǎn)化為拼音,英文字符不變
*
* @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悦析、漢字轉(zhuǎn)換位漢語(yǔ)拼音首字母寿桨,英文字符不變
/**
* 漢字轉(zhuǎn)換位漢語(yǔ)拼音首字母,英文字符不變
*
* @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}]+", ""); // 正則去掉所有字符操作
// 正則表達(dá)式去掉所有中文的特殊符號(hào)
String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~亭螟!@#¥%……&*()——+|{}<>《》【】‘;:”“’骑歹。预烙,、道媚?]";
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(chines);
chines = matcher.replaceAll("").trim();
return chines;
}
測(cè)試:
public static void main(String[] args) {
System.out.println("==獲得漢字首字母:======="+getAlpha("測(cè)試"));
System.out.println("==獲得漢語(yǔ)拼音首字母:======="+getAlphaDefaultCode("測(cè)試一下"));
System.out.println("==中文轉(zhuǎn)化為拼音,英文字符不變:======="+getPingYin("測(cè)試一下test"));
System.out.println("==漢語(yǔ)拼音首字母扁掸,英文字符不變:======="+converterToFirstSpell("測(cè)試一下test"));
System.out.println("==清理特殊字符:======="+cleanChar("測(cè)試一下2!@#¥%……SDmm"));
}
}
結(jié)果:
測(cè)
試
==獲得漢字首字母:=======CS
==獲得漢語(yǔ)拼音首字母:=======CSYX
==中文轉(zhuǎn)化為拼音,英文字符不變:=======ceshiyixiatest
==漢語(yǔ)拼音首字母最域,英文字符不變:=======CSYXtest
==清理特殊字符:=======測(cè)試一下2SDmm
Process finished with exit code 0