根据字符串生成首字母

/**
* 汉字匹配拼音对照

* @param chinese
* @return
*/
private static String matchPinYin(String chinese, boolean needConvert) {
String chineseTemp = chinese;
if (needConvert) {
chinese = conversionStr(chinese, "ISO8859-1", "GB2312");
}
chinese = chinese.substring(0, 1);


// findRepeatWord(exceptWords);


for (Entry<String, String> letterSet : exceptWords.entrySet()) {
if (letterSet.getValue().indexOf(chinese) != -1) {
chinese = letterSet.getKey();
break;
}
}
chinese = chineseTemp.equals(chinese) ? "?" : chinese;
return chinese;
}


private static String matchPinYin(String chinese) {
return matchPinYin(chinese, true);
}


/**
* 字符串编码转换

* @param str 要转换编码的字符串
* @param charsetName 原来的编码
* @param toCharsetName 转换后的编码
* @return 经过编码转换后的字符串
*/
private static String conversionStr(String str, String charsetName, String toCharsetName) {
try {
str = new String(str.getBytes(charsetName), toCharsetName);
} catch (UnsupportedEncodingException ex) {
System.out.println("字符串编码转换异常:" + ex.getMessage());
}
return str;
}

猜你喜欢

转载自blog.csdn.net/hello_world_wsm/article/details/80978248