public class InitialUtil {
/**
* 提取每个汉字的首字母(大写)
*
* @param str
* @return
*/
public static String getPinYinHeadChar(String str) {
if (isNull(str)) {
return "";
}
//去除非汉字得字符
str = removeUnChinese(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;
}
}
convert = string2AllTrim(convert);
return convert.toLowerCase();
}
public static String removeUnChinese(String str) {
return str.replaceAll("[^\u4E00-\u9FA5]", "");
}
/*
* 判断字符串是否为空
*/
public static boolean isNull(Object strData) {
if (strData == null || String.valueOf(strData).trim().equals("")) {
return true;
}
return false;
}
/**
* 去掉字符串包含的所有空格
*
* @param value
* @return
*/
public static String string2AllTrim(String value) {
if (isNull(value)) {
return "";
}
return value.trim().replace(" ", "");
}
public static void main(String[] args) {
String str="焕然一新|我们的官网全新改版上线啦!";
String pinYinHeadChar = getPinYinHeadChar(str);
System.out.println(pinYinHeadChar);
}
}
Java获取中文字符窜首字母得工具类
最新推荐文章于 2023-11-14 14:31:17 发布