package com.anxin.ssk.common; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; /** * 取得给定汉字串的首字母串,即声母串 Title: ChineseCharToEn * * @author: (le.qiao) * @e-mail: qiaolevip@gmail.com * @myblog: <a href="http://qiaolevip.iteye.com">http://qiaolevip.iteye.com</a> * @date: 2014-1-15 注:只支持GB2312字符集中的汉字 * */ public class LetterUtil { private final static 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 static 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" }; /** * 取得给定汉字串的首字母串,即声母串 * * @param str 给定汉字串 * @return 声母串 */ public static String getAllFirstLetter(String str) { if (str == null || str.trim().length() == 0) { return ""; } String _str = ""; for (int i = 0; i < str.length(); i++) { _str = _str + getFirstLetter(str.substring(i, i + 1)); } return _str; } /** * 取得给定汉字的首字母,即声母 * * @param chinese 给定的汉字 * @return 给定汉字的声母 */ public static String getFirstLetter(String chinese) { if (chinese == null || chinese.trim().length() == 0) { return ""; } chinese = conversionStr(chinese, "GB2312", "ISO8859-1"); if (chinese.length() > 1) // 判断是不是汉字 { int li_SectorCode = (int) chinese.charAt(0); // 汉字区码 int li_PositionCode = (int) chinese.charAt(1); // 汉字位码 li_SectorCode = li_SectorCode - 160; li_PositionCode = li_PositionCode - 160; int li_SecPosCode = li_SectorCode * 100 + li_PositionCode; // 汉字区位码 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]; break; } } } else // 非汉字字符,如图形符号或ASCII码 { chinese = conversionStr(chinese, "ISO8859-1", "GB2312"); chinese = chinese.substring(0, 1); Map<String, String> exceptWords = new HashMap<String, String>(); // 欢迎大家补充说明~欢迎大家补充说明~欢迎大家补充说明 exceptWords.put("a", "庵"); exceptWords.put("g", "崮"); exceptWords.put("l", "崂"); exceptWords.put("j", "泾"); exceptWords.put("r", "榕"); exceptWords.put("x", "鑫"); exceptWords.put("y", "懿"); exceptWords.put("z", "梓"); for (Entry<String, String> letterSet : exceptWords.entrySet()) { if (letterSet.getValue().indexOf(chinese) != -1) { chinese = letterSet.getKey(); break; } } } } return chinese; } /** * 字符串编码转换 * * @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; } public static void main(String[] args) { System.out.println("获取拼音首字母:" + LetterUtil.getAllFirstLetter("泾崂")); String address = "(金浜小区)栖山路1689弄"; address = address.substring(address.indexOf(")") + 1); System.out.println("获取拼音首字母:" + LetterUtil.getFirstLetter(address)); } }
相关推荐
汉字转拼音,可以大小写、全拼、首字母,注释清晰明了。修改好的发给大家学习学习
pinyin4J java拼音转换工具 java汉字转换拼音工具
基于java的开发源码-中文转拼音库 pinyin4j.zip 基于java的开发源码-中文转拼音库 pinyin4j.zip 基于java的开发源码-中文转拼音库 pinyin4j.zip 基于java的开发源码-中文转拼音库 pinyin4j.zip 基于java的开发源码-...
支持2万+汉字转拼音首字母
java安卓android汉字转拼音中文转拼音pinyin4j简化精简版. 网上流传的有180KB,太大了,对于android开发来说。我精简了很多无关内容,收录常用汉字大约2万个,完全够用了,还专门写了一个helper类,直接调用这个...
完整的汉字转拼音代码,可以转多种类型,转全拼音或者转第一个字为全拼音,后面取首字母和Jar包
Java实现汉字转拼音功能,包括 首字母和全拼 for (int j = 0, int len = str.length(); j ; j++) { char word = str.charAt(j); String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word); if ...
汉字转换拼音、首字母大写 。
java版本汉字转拼音库pinyin4j,以及pinyin4j中用到的sparta库,都是源码。
安卓汉字转拼音 联系人字母城市索引jar包 pinyinHelper pinyin4j-2.5.0.jar
汉字转全拼音或拼音首字母,支持gb2312和utf8编码 ,附带函数说明, 汉字转换为全拼音或拼音首字母 使用方法: include(ROOT_PATH.'includes/cls_chart_pinyin.php'); echo Pinyin('中国人','utf-8',false); 函数...
pinyin4j-2.5.0.jar用于将汉字转拼音,用于实际项目汉字排序、汉字分类等功能
汉字转拼音jar包附带封装 适用于Android和Java
此文档完全为个人总结,文档中以截图的方式呈现代码,希望会对大家有所帮助。
pinyin4j-2.5.0.jar 中文转化拼音,获取首字母
java源码:中文转拼音库 pinyin4j.zip
标签:belerweb、pinyin4j、jar包、java、API文档、中文版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,...
Java中,汉语转换成拼音的jar包。放到项目下,直接引用pinyin4j-2.5.0.jar就行。或者直接放到maven本地资源文件下解压压缩包直接再项目配置文件中引用。
Java pinyin4j.jar中文转拼音 详细可查看我的博客https://me.csdn.net/Extraordinarylife 中《Java pinyin4j.jar中文转拼音 》