import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; public class Chinesetoen { private final int[] li_SecPosValue = { 1601, 1637, 1833, 2078, 2274, 2302, 2433, 2594, 2787, 3106, 3212, 3472, 3635, 3722, 3730, 3858, 4027, 4086, 4390, 4558, 4684, 4925, 5249, 5590 }; private final String[] lc_FirstLetter = { "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "w", "x", "y", "z" }; private final String ls_SecondSecTable = "CJWGNSPGCGNE[Y[BTYYZDXYKYGT[JNNJQMBSGZSCYJSYY[PGKBZGY[YWJKGKLJYWKPJQHY[W[DZLSGMRYPYWWCCKZNKYYGTTNJJNYKKZYTCJNMCYLQLYPYQFQRPZSLWBTGKJFYXJWZLTBNCXJJJJTXDTTSQZYCDXXHGCK[PHFFSS[YBGXLPPBYLL[HLXS[ZM[JHSOJNGHDZQYKLGJHSGQZHXQGKEZZWYSCSCJXYEYXADZPMDSSMZJZQJYZC[J[WQJBYZPXGZNZCPWHKXHQKMWFBPBYDTJZZKQHYLYGXFPTYJYYZPSZLFCHMQSHGMXXSXJ[[DCSBBQBEFSJYHXWGZKPYLQBGLDLCCTNMAYDDKSSNGYCSGXLYZAYBNPTSDKDYLHGYMYLCXPY[JNDQJWXQXFYYFJLEJPZRXCCQWQQSBNKYMGPLBMJRQCFLNYMYQMSQYRBCJTHZTQFRXQHXMJJCJLXQGJMSHZKBSWYEMYLTXFSYDSWLYCJQXSJNQBSCTYHBFTDCYZDJWYGHQFRXWCKQKXEBPTLPXJZSRMEBWHJLBJSLYYSMDXLCLQKXLHXJRZJMFQHXHWYWSBHTRXXGLHQHFNM[YKLDYXZPYLGG[MTCFPAJJZYLJTYANJGBJPLQGDZYQYAXBKYSECJSZNSLYZHSXLZCGHPXZHZNYTDSBCJKDLZAYFMYDLEBBGQYZKXGLDNDNYSKJSHDLYXBCGHXYPKDJMMZNGMMCLGWZSZXZJFZNMLZZT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DMXMPHJSGZGSZZQLYLWTJPFSYASMCJBTZKYCWMYTCSJJLJCQLWZMALBXYFBPNLSFHTGJWEJJXXGLLJSTGSHJQLZFKCGNNNSZFDEQFHBSAQTGYLBXMMYGSZLDYDQMJJRGBJTKGDHGKBLQKBDMBYLXWCXYTTYBKMRTJZXQJBHLMHMJJZMQASLDCYXYQDLQCAFYWYXQHZ "; int li_offset; /** * \u53D6\u5F97\u7ED9\u5B9A\u6C49\u5B57\u4E32\u7684\u9996\u5B57\u6BCD\u4E32,\u5373\u58F0\u6BCD\u4E32 * * @param str * \u7ED9\u5B9A\u6C49\u5B57\u4E32 * @return \u58F0\u6BCD\u4E32 */ public String getAllFirstLetter(String str) { if (str == null || str.trim().length() == 0) { return ""; } String _str = ""; for (int i = 0; i < str.length(); i++) { _str = _str + this.getFirstLetter(str.substring(i, i + 1)); } return _str; } /** * \u53D6\u5F97\u7ED9\u5B9A\u6C49\u5B57\u7684\u9996\u5B57\u6BCD,\u5373\u58F0\u6BCD * * @param chinese * \u7ED9\u5B9A\u7684\u6C49\u5B57 * @return \u7ED9\u5B9A\u6C49\u5B57\u7684\u58F0\u6BCD */ //\u8F93\u5165\u5B57\u7B26\u4E32\u6C49\u5B57\uFF0C\u8FD4\u56DE\u5B57\u7B26\u4E32\uFF0C\u5982\u679C\u6709\u6C49\u5B57\uFF0C\u5C06\u6C49\u5B57\u7684\u9996\u5B57\u6BCD\u7684\u8FD4\u56DE private String getFirstLetter(String chinese) { if (chinese == null || chinese.trim().length() == 0) { return ""; } chinese = conversionStr(chinese, "GB2312", "ISO8859-1"); if (chinese.length() > 1) // \u5224\u65AD\u662F\u4E0D\u662F\u6C49\u5B57 { int li_SectorCode = (int) chinese.charAt(0); // \u6C49\u5B57\u533A\u7801 int li_PositionCode = (int) chinese.charAt(1); // \u6C49\u5B57\u4F4D\u7801 li_SectorCode = li_SectorCode - 160; li_PositionCode = li_PositionCode - 160; int li_SecPosCode = li_SectorCode * 100 + li_PositionCode; // \u6C49\u5B57\u533A\u4F4D\u7801 if (li_SecPosCode > 1600 && li_SecPosCode < 5590) { for (int i = 0; i < 23; i++) { if (li_SecPosCode >= li_SecPosValue[i] && li_SecPosCode < li_SecPosValue[i + 1]) { chinese = lc_FirstLetter[i].toUpperCase(); break; } } } else { // \u7B2C\u4E00\u4E2A\u5B57\u7B26 li_offset = (li_SectorCode - 56) * 94 + li_PositionCode - 1; // \u8BA1\u7B97\u504F\u79FB\u91CF if (li_offset >= 0 && li_offset <= 3007) { // \u4E8C\u533A\u6C49\u5B57 chinese = ls_SecondSecTable.substring(li_offset, li_offset + 1); // \u53D6\u51FA\u6B64\u5B57\u58F0\u6BCD } else // \u975E\u6C49\u5B57\u5B57\u7B26,\u5982\u56FE\u5F62\u7B26\u53F7\u6216ASCII\u7801 { chinese = conversionStr(chinese, "ISO8859-1", "GB2312"); chinese = chinese.substring(0, 1); } } } return chinese; } /** * \u5B57\u7B26\u4E32\u7F16\u7801\u8F6C\u6362 * * @param str * \u8981\u8F6C\u6362\u7F16\u7801\u7684\u5B57\u7B26\u4E32 * @param charsetName * \u539F\u6765\u7684\u7F16\u7801 * @param toCharsetName * \u8F6C\u6362\u540E\u7684\u7F16\u7801 * @return \u7ECF\u8FC7\u7F16\u7801\u8F6C\u6362\u540E\u7684\u5B57\u7B26\u4E32 */ private static String conversionStr(String str, String charsetName, String toCharsetName) { try { str = new String(str.getBytes(charsetName), toCharsetName); } catch (UnsupportedEncodingException ex) { System.out.println("\u5B57\u7B26\u4E32\u7F16\u7801\u8F6C\u6362\u5F02\u5E38\uFF1A" + ex.getMessage()); } return str; } public static void main(String[] args) throws IOException { String str="\u4F60\u8FD8\u597D\u5417"; String encoder=URLEncoder.encode(str,"utf-8"); System.out.println(encoder); String decode=URLDecoder.decode(encoder,"utf-8"); System.out.println(decode); Chinesetoen chinesetoen=new Chinesetoen(); System.out.println(chinesetoen.getAllFirstLetter(decode).toUpperCase()); } }