一、前言
将中文汉字字符串转为每个字的中文拼音的首字母(即声母串),先把汉字匹配拼音对照进行转换,再获取每个汉字首个字母拼接串起来,详情代码示例。
二、代码示例
import java.io.UnsupportedEncodingException;@b@import java.util.HashMap;@b@import java.util.Map;@b@import java.util.Map.Entry;@b@@b@public class ChineseEnUtils{@b@@b@ private final static int[] li_SecPosValue = { 1601, 1637, 1833, 2078, 2274,@b@ 2302, 2433, 2594, 2787, 3106, 3212, 3472, 3635, 3722, 3730, 3858,@b@ 4027, 4086, 4390, 4558, 4684, 4925, 5249, 5590 }; @b@ @b@ private final static String[] lc_FirstLetter = { "a", "b", "c", "d", "e",@b@ "f", "g", "h", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",@b@ "t", "w", "x", "y", "z" };@b@ @b@ private static Map<String,String> exceptWords=new HashMap<String, String>();@b@ @b@ @b@ static{@b@ exceptWords.put("a", "庵鳌"); @b@ exceptWords.put("b", "璧亳並侼別匂蝙"); @b@ exceptWords.put("c", "茌丞丒丳刅"); @b@ exceptWords.put("d", "渎砀棣儋丟"); @b@ exceptWords.put("e", ""); @b@ exceptWords.put("f", "邡冹兝蝠"); @b@ exceptWords.put("g", "崮藁莞丐丱乢亁仠冮匃匄"); @b@ exceptWords.put("h", "骅珲潢湟丆冴匢"); @b@ exceptWords.put("j", "泾蛟暨缙旌莒鄄丌丩丮丯丼亅伋冏匊匛匞"); @b@ exceptWords.put("k", "丂匟"); @b@ exceptWords.put("l", "崂涞栾溧漯浏耒醴泸阆崃両刢劽啰"); @b@ exceptWords.put("m", "渑汨丏冐冺兞冇"); @b@ exceptWords.put("n", ""); @b@ exceptWords.put("o", "瓯"); @b@ exceptWords.put("p", "邳濮郫丕伂冸"); @b@ exceptWords.put("q", "喬綦衢岐朐邛丠丬亝冾兛匤"); @b@ exceptWords.put("r", "榕刄"); @b@ exceptWords.put("s", "泗睢沭嵊歙莘嵩鄯丄丗侺兙"); @b@ exceptWords.put("t", "潼滕郯亣侹侻"); @b@ exceptWords.put("w", "婺涠汶亾仼卍卐"); @b@ exceptWords.put("x", "鑫盱浔荥淅浠亵丅伈兇"); @b@ exceptWords.put("y", "懿眙黟颍兖郓偃鄢晏丣亜伇偐円匜"); @b@ exceptWords.put("z", "梓涿诏柘秭圳伀冑刣");@b@ }@b@ @b@ /**@b@ * 取得给定汉字串的首字母串,即声母串 @b@ * @param str 给定汉字串@b@ * @return 声母串@b@ */@b@ public static String getAllFirstLetter(String str) {@b@ if (str == null || str.trim().length() == 0) {@b@ return "";@b@ }@b@ String _str = "";@b@ for (int i = 0; i < str.length(); i++) {@b@ _str = _str + getFirstLetter(str.substring(i, i + 1));@b@ }@b@ return _str;@b@ }@b@ @b@ /**@b@ * 取得给定汉字的首字母,即声母 @b@ * @param chinese 给定的汉字@b@ * @return 给定汉字的声母@b@ */@b@ public static String getFirstLetter(String chinese) {@b@ if (chinese == null || chinese.trim().length() == 0) {@b@ return "";@b@ }@b@ chinese = conversionStr(chinese, "GB2312", "ISO8859-1");@b@ if (chinese.length() > 1) // 判断是不是汉字@b@ {@b@ int li_SectorCode = (int) chinese.charAt(0); // 汉字区码@b@ int li_PositionCode = (int) chinese.charAt(1); // 汉字位码@b@ li_SectorCode = li_SectorCode - 160;@b@ li_PositionCode = li_PositionCode - 160;@b@ int li_SecPosCode = li_SectorCode * 100 + li_PositionCode; // 汉字区位码@b@ if (li_SecPosCode > 1600 && li_SecPosCode < 5590) {@b@ for (int i = 0; i < 23; i++) {@b@ if (li_SecPosCode >= li_SecPosValue[i]@b@ && li_SecPosCode < li_SecPosValue[i + 1]) {@b@ chinese = lc_FirstLetter[i];@b@ break;@b@ }@b@ }@b@ } else // 非汉字字符,如图形符号或ASCII码@b@ {@b@ // chinese = this.conversionStr(chinese, "ISO8859-1", "GB2312");@b@ // chinese = chinese.substring(0, 1);@b@ chinese=matchPinYin(chinese);@b@ }@b@ }@b@ @b@ //如果还是无法匹配,再次进行拼音匹配@b@ if(chinese.equals("?"))@b@ chinese=matchPinYin(chinese,false);@b@ @b@ return chinese;@b@ }@b@ @b@ /**@b@ * 汉字匹配拼音对照@b@ * @param chinese@b@ * @param needConvert@b@ * @return@b@ */@b@ private static String matchPinYin(String chinese,boolean needConvert){@b@ String chineseTemp=chinese;@b@ if(needConvert){@b@ chinese=conversionStr(chinese, "ISO8859-1", "GB2312");@b@ }@b@ chinese=chinese.substring(0,1);@b@ @b@ for (Entry<String,String> letterSet : exceptWords.entrySet()) {@b@ if(letterSet.getValue().indexOf(chinese)!=-1){@b@ chinese=letterSet.getKey();@b@ break;@b@ }@b@ }@b@ chinese=chineseTemp.equals(chinese)?"?":chinese;@b@ return chinese;@b@ }@b@ @b@ private static String matchPinYin(String chinese){@b@ return matchPinYin(chinese,true);@b@ }@b@ @b@ /**@b@ * 字符串编码转换 @b@ * @param str 要转换编码的字符串@b@ * @param charsetName 原来的编码@b@ * @param toCharsetName 转换后的编码@b@ * @return 经过编码转换后的字符串@b@ */@b@ private static String conversionStr(String str, String charsetName,@b@ String toCharsetName) {@b@ try {@b@ str = new String(str.getBytes(charsetName), toCharsetName);@b@ } catch (UnsupportedEncodingException ex) {@b@ System.out.println("字符串编码转换异常:" + ex.getMessage());@b@ }@b@ return str;@b@ }@b@ @b@ public static void main(String[] args) {@b@ System.out.println("获取字符串每个汉字拼音首字母:" + ChineseEnUtils.getAllFirstLetter("蝙蝠顶顶顶顶顶顶顶顶撒发生访问服务的说法是"));@b@ System.out.println("获取拼音首字母:" + ChineseEnUtils.getFirstLetter("蝙蝠"));@b@ }@b@@b@}
控制台打印结果
获取拼音首字母:bfddddddddsfsfwfwdsfs@b@获取拼音首字母:b