java 实现根据汉字生成拼音全拼或拼音首字母

发布时间:2020-11-03 15:55:59 作者:Leah
来源:亿速云 阅读:236

今天就跟大家聊聊有关java 实现根据汉字生成拼音全拼或拼音首字母,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

1.情景展示

  java 根据中文生成对应的拼音 

2.准备工作

  所需jar包:pinyin4j-2.5.0.jar   

3.解决方案

  导包

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.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

  代码实现

/**
 * 根据汉字生成拼音全拼或拼音首字母
 * @explain
 * @author Marydon
 * @creationTime 2020年5月14日下午4:26:30
 * @version 1.0
 * @since
 * @email marydon20170307@163.com
 */
public class GetPinyin {
    /**
     * 得到全拼
     * @param str
     * @return 全拼(小写)
     */
    public static String getPinYin(String str){
        char t1[]=null;
        t1=str.toCharArray();
        String[] t2=new String[t1.length];
        HanyuPinyinOutputFormat t3=new HanyuPinyinOutputFormat();
        t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        t3.setVCharType(HanyuPinyinVCharType.WITH_V);
        String t4="";
        int t0=t1.length;
        try {
            for ( int i = 0; i < t0; i++ ) {
                //是用来判断是不是中文的一个条件,采用的是unicode编码
                if(Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")){
                    t2= PinyinHelper.toHanyuPinyinStringArray(t1[i],t3);
                    t4+=t2[0];
                }else {
                    t4+=Character.toString(t1[i]);
                }
            }
            return t4;
        } catch ( BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination ) {
            badHanyuPinyinOutputFormatCombination.printStackTrace();
        }
        return t4;
    }
 
    /**
     * 得到汉字首字母的拼音
     * @param str
     * @return 拼音首字母(大写)
     */
    public static String getPinYinHeaderChar(String str){
        String convert="";
        for ( int i = 0; i < str.length(); i++ ) {
            char word=str.charAt(i);
            String[] pinYinArray=PinyinHelper.toHanyuPinyinStringArray(word);
            if ( pinYinArray!=null ){
                convert+=pinYinArray[0].charAt(0);
            }else {
                convert+=word;
            }
        }
        return convert.toUpperCase();
    }
 
  //测试
    public static void main(String[] args) {
        System.out.println(getPinYin("火影忍者Marydon"));
        System.out.println(getPinYinHeaderChar("海贼王"));
    }
}

4.效果展示 

java 实现根据汉字生成拼音全拼或拼音首字母

看完上述内容,你们对java 实现根据汉字生成拼音全拼或拼音首字母有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

推荐阅读:
  1. springboot shiro 权限管理 后台框架源码 java 项目
  2. spring boot 权限管理 后台框架源码 java 项目 shiro

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java ava

上一篇:使用python如何实现绘制疫情图

下一篇:SpringBoot中实现配置Web静态资源路径的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》