/**
* 获得字符串的拼写+简写
* @param s
* @return ce试xing名-》ceshixingmingcsxm 测试姓名01->ceshixingming01cesxingm01
* 单二->shanerdanerchanersedece 单二01->shaner01daner01chaner01se01de01ce01
*/
public static String getSpell(String s){
if (isChinese(s)) {
String[] spell ;
String[] cs = s.split("");
List<List<String>> jxs = new ArrayList<List<String>>();
List<List<String>> pys = new ArrayList<List<String>>();
for(String c : cs){
List<String> jx = new ArrayList<String>();
List<String> py = new ArrayList<String>();
if(isChinese(c)){
// 转换成 集合 单-》["shan","dan","chan"]
spell = PinyinHelper.toHanyuPinyinStringArray(c.charAt(0));
for(String __s : spell){
//去掉音调 存入拼音集合
py.add(__s.substring(0, __s.length() - 1));
//截取首字母 存入简写集合
jx.add(__s.substring(0,1));
}
}else{
py.add(c);
jx.add(c);
}
pys.add(py);
jxs.add(jx);
}
return getPY("", pys) + getPY("", jxs);
}else{
return s;
}
}
private static String getPY(String s,List<List<String>> ls){
if (ls.size() > 0) {
List<String> l = ls.get(0);
List<List<String>> _ls = getArrayCopy(ls);
if (_ls.size() > 0) {
for (String _s : l)
s = getPY(s + _s,_ls);
}else{
for (String _s : l)
s += _s;
}
}
return s;
}
private static List<List<String>> getArrayCopy(List<List<String>> arr){
List<List<String>> b = new ArrayList<List<String>>(arr.size() - 1);
for(int i=1;i<arr.size();i++)
b.add(arr.get(i));
return b;
}
private static String regEx = "[\\u4e00-\\u9fa5]+";
public static boolean isChinese(String str){
return Pattern.compile(regEx).matcher(str).find();
}
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.1</version>
</dependency>