一、前言
将中文汉字字符串转为每个字的中文拼音的首字母(即声母串),先把汉字匹配拼音对照进行转换,再获取每个汉字首个字母拼接串起来,详情代码示例。
二、代码示例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 exceptWords=new HashMap();@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 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 = li_SecPosValue[i]@b@&& li_SecPosCode 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