使用PinYin4j,获取汉字的拼音字母

需要导入的文件

<!-- 引入pinyin4J的依赖 -->
        <dependency>
            <groupId>com.belerweb</groupId>
            <artifactId>pinyin4j</artifactId>
            <version>2.5.0</version>
        </dependency>
            
@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[] headString = PinYin4jUtils.getHeadByString(info);
        String shortCode = StringUtils.join(headString);
        System.out.println(shortCode);
        
        //城市编码 --->> shijiahzuang
        String cityCode = PinYin4jUtils.hanziToPinyin(city,"");
        System.out.println(cityCode);
    }

控制台打印:

HBSJZQX
shijiazhuang

猜你喜欢

转载自www.cnblogs.com/FanJava/p/9261954.html