zoukankan      html  css  js  c++  java
  • java实现将汉字转为首字母、拼音

    本文转自java实现将汉字转为拼音
    作者itRed
    本人仅稍作整理,并提出一些问题。问题的话暂时没时间处理,等以后有时间了再更新。


    测试参数
    String info="汉字转换为拼音";

    结果
    HZZHWPY --- 全部大写需要.toUpperCase()
    hanzizhuanhuanweipinyin

    步骤

    1、Maven中引入依赖

    <!-- https://mvnrepository.com/artifact/com.belerweb/pinyin4j -->
    <dependency>
        <groupId>com.belerweb</groupId>
        <artifactId>pinyin4j</artifactId>
        <version>2.5.1</version>
    </dependency>
    

    2、定义方法

    /**
     * 获取字符串拼音的第一个字母
     * @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、调用方法

    public static void main(String[] args) {
        System.out.println(ToFirstChar("汉字转换为拼音").toUpperCase()); //转为首字母大写
        System.out.println(ToPinyin("汉字转换为拼音")); 
    }
    

    问题

    今天(2017-9-21 19:38:29)工作上有这个需求,直接把原文的代码拿过来用了。
    但是有个问题,如果字符串中有符号,那么就会报错。
    因为我的数据中符号比较简单,都是 () ()括号之类的,所以我直接加了个简单的判断,使用replace把括号拿掉了,不过还是要考虑一下出现大量符号的情况。

    先记录下,以后有时间了再来处理这个。

  • 相关阅读:
    datagridview中读取数据判断+考勤每月上班天数判断
    dateTimePicker日期比较+时间段内查询+员工查询薪资步骤+datagridview
    c#word 存取
    位图去空白
    过桥问题
    Dominos 2(DFS)(容器)
    poj 3421(三分)
    poj 3186(DP)
    安装Ubuntu
    poj 3273(二分)
  • 原文地址:https://www.cnblogs.com/VitoYi/p/7570670.html
Copyright © 2011-2022 走看看