pinyin4j的基本使用
PinYin4jUtils工具类代码:http://www.cnblogs.com/jepson6669/p/8856082.html
maven中引入依赖
<!-- 引入pinyin4J的依赖 --> <dependency> <groupId>com.belerweb</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.0</version> </dependency>
测试
package com.test.bos.test; import org.apache.commons.lang3.StringUtils; import org.junit.Test; import com.test.bos.utils.PinYin4JUtils; public class PinYin4JTest { @Test public void test() { // 河北省 石家庄市 桥西区 String province = "河北省"; String city = "石家庄市"; String district = "桥西区"; // 简码---->>HBSJZQX province = province.substring(0, province.length() - 1); city = city.substring(0, city.length() - 1); district = district.substring(0, district.length() - 1); String info = province + city + district; String[] headByString = PinYin4JUtils.getHeadByString(info); //使用StringUtils.join方法将[H,B,S,J,Z,Q,X]连接到一起成为HBSJZQX String shortcode = StringUtils.join(headByString); System.out.println(shortcode); // 城市编码---->>shijiazhuang String citycode = PinYin4JUtils.hanziToPinyin(city, ""); System.out.println(citycode); } }