首页 > 代码库 > 汉字转拼音

汉字转拼音

 

1、需要jar包:

gradle引入:

  compile group: ‘com.belerweb‘, name: ‘pinyin4j‘, version: ‘2.5.1‘

搜索jar包网址:http://mvnrepository.com 

 

2、代码:

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dayhro.base.entity.SysDictionary;
import com.dayhro.base.service.SysDictionaryService;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

@Controller
@RequestMapping("/pinyin")
public class PinyinDemo {
    
    @Resource
    private SysDictionaryService sysDictionaryService;
    
    /**
     * 测试main方法
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("首字母拼音:"+ToFirstChar("长沙").toUpperCase()); //转为首字母大写
        System.out.println("全拼:"+ToPinyin("长沙")); 
        
    }
    
    @RequestMapping("/batchInsert")
    public void batchInsert(){
        
        List<SysDictionary> list = new ArrayList<SysDictionary>();
        
     //全国所有市 String[] city
= {"石家庄","唐山","秦皇岛","邯郸","邢台","保定","张家口","承德","沧州","廊坊","衡水","太原","大同","阳泉","长治","晋城",
        "朔州","晋中","运城","忻州","临汾","吕梁","呼和浩特","包头","乌海","赤峰","通辽","鄂尔多斯","呼伦贝尔","巴彦淖尔","乌兰察布",
        "兴安盟","锡林郭勒盟","阿拉善盟","沈阳","大连","鞍山","抚顺","本溪","丹东","锦州","营口","阜新","辽阳","盘锦","铁岭","朝阳",
        "葫芦岛","长春","吉林","四平","辽源","通化","白山","松原","白城","延边朝鲜族","哈尔滨","齐齐哈尔","鸡西","鹤岗","双鸭山","大庆",
        "伊春","佳木斯","七台河","牡丹江","黑河","绥化","大兴安岭","南京","无锡","徐州","常州","苏州","南通","连云港","淮安","盐城","扬州",
        "镇江","泰州","宿迁","杭州","宁波","温州","嘉兴","湖州","绍兴","金华","衢州","舟山","台州","丽水","合肥","芜湖","蚌埠","淮南",
        "马鞍山","淮北","铜陵","安庆","黄山","滁州","阜阳","宿州","巢湖","六安","亳州","池州","宣城","福州","厦门","莆田","三明","泉州",
        "漳州","南平","龙岩","宁德","南昌","景德镇","萍乡","九江","新余","鹰潭","赣州","吉安","宜春","抚州","上饶","济南","青岛","淄博",
        "枣庄","东营","烟台","潍坊","济宁","泰安","威海","日照","莱芜","临沂","德州","聊城","滨州","荷泽","郑州","开封","洛阳","平顶山",
        "安阳","鹤壁","新乡","焦作","濮阳","许昌","漯河","三门峡","南阳","商丘","信阳","周口","驻马店","武汉","黄石","十堰","宜昌","襄樊",
        "鄂州","荆门","孝感","荆州","黄冈","咸宁","随州","恩施土家族苗族","长沙","株洲","湘潭","衡阳","邵阳","岳阳","常德","张家界","益阳",
        "郴州","永州","怀化","娄底","湘西土家族苗族","广州","韶关","深圳","珠海","汕头","佛山","江门","湛江","茂名","肇庆","惠州","梅州",
        "汕尾","河源","阳江","清远","东莞","中山","潮州","揭阳","云浮","南宁","柳州","桂林","梧州","北海","防城港","钦州","贵港","玉林",
        "百色","贺州","河池","来宾","崇左","海口","三亚","成都","自贡","攀枝花","泸州","德阳","绵阳","广元","遂宁","内江","乐山","南充",
        "眉山","宜宾","广安","达州","雅安","巴中","资阳","阿坝藏族羌族","甘孜藏族","凉山彝族","贵阳","六盘水","遵义","安顺","铜仁","黔西南布依族苗族",
        "毕节","黔东南苗族侗族","黔南布依族苗族","昆明","曲靖","玉溪","保山","昭通","丽江","思茅","临沧","楚雄彝族","红河哈尼族彝族","文山壮族苗族",
        "西双版纳傣族","大理白族","德宏傣族景颇族","怒江傈僳族","迪庆藏族","拉萨","昌都","山南","日喀则","那曲","阿里","林芝","西安","铜川","宝鸡",
        "咸阳","渭南","延安","汉中","榆林","安康","商洛","兰州","嘉峪关","金昌","白银","天水","武威","张掖","平凉","酒泉","庆阳","定西","陇南","临夏回族",
        "甘南藏族","西宁","海东","海北藏族","黄南藏族","海南藏族","果洛藏族","玉树藏族","海西蒙古族藏族","银川","石嘴山","吴忠","固原","中卫","乌鲁木齐",
        "克拉玛依","吐鲁番","哈密","昌吉回族","博尔塔拉蒙古","巴音郭楞蒙古","阿克苏","克孜勒苏柯尔克孜","喀什","和田","伊犁哈萨克","塔城","阿勒泰"}; String firstLetter = ""; String fullPinyin = ""; for(int i=0;i<city.length;i++){ firstLetter = ToFirstChar(city[i]).toUpperCase(); //每个汉字的首字母 fullPinyin = ToPinyin(city[i]); //全拼 SysDictionary sys = new SysDictionary(); sys.setUuid(Long.valueOf(400+i+1)); sys.setParentId(1L); if(i<=8){ sys.setCode("001-00"+(i+1)); }else if(i>8 && i<=98){ sys.setCode("001-0"+(i+1)); }else if(i>98){ sys.setCode("001-"+(i+1)); } sys.setName(city[i]); sys.setOrderNum(i+1); sys.setCorpId(0L); sys.setCreator(1L); sys.setUpdater(1L); sys.setCreateTime("2016-10-25"); sys.setModifyTime("2016-10-25"); sys.setFirstLetter(firstLetter); sys.setFullPinyin(fullPinyin); list.add(sys); System.out.println(sys.getCode()+ "--" + sys.getUuid() + "--" +city[i] +"--" + firstLetter + "--" + fullPinyin); } //sysDictionaryService.batchInsert(list); //批量插入 } /** * 获取字符串拼音的第一个字母 * @param chinese * @return */ public static String ToFirstChar(String chinese){ String pinyinStr = ""; char[] newChar = chinese.toCharArray(); //转为单个字符 HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); for (int i = 0; i < newChar.length; i++) { if (newChar[i] > 128) { try { pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0); } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } }else{ pinyinStr += newChar[i]; } } return pinyinStr; } /** * 汉字转为拼音 * @param chinese * @return */ public static String ToPinyin(String chinese){ String pinyinStr = ""; char[] newChar = chinese.toCharArray(); HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); for (int i = 0; i < newChar.length; i++) { if (newChar[i] > 128) { try { pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0]; } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } }else{ pinyinStr += newChar[i]; } } return pinyinStr; } }

 

3、批量插入语句:

    <!-- 批量导入 -->
    <insert id="batchInsert" parameterType="java.util.List" >
        insert into t_hro_xxx(
            uuid, parent_id, code, name, order_num, corp_id, 
            creator, updater, create_time, modify_time, first_letter, full_pinyin)
        values
        <foreach collection="list" item="item" index="index" separator="," >
        (
            #{item.uuid,jdbcType=BIGINT},  #{item.parentId,jdbcType=BIGINT}, #{item.code,jdbcType=VARCHAR}, 
            #{item.name,jdbcType=VARCHAR}, #{item.orderNum,jdbcType=INTEGER},  #{item.corpId,jdbcType=BIGINT},  
            #{item.creator,jdbcType=BIGINT}, #{item.updater,jdbcType=BIGINT}, #{item.createTime,jdbcType=VARCHAR},
            #{item.modifyTime,jdbcType=VARCHAR}, #{item.firstLetter,jdbcType=VARCHAR}, #{item.fullPinyin,jdbcType=VARCHAR}
        )
        </foreach>
    </insert>

 

4、打印语句:

技术分享

 

汉字转拼音