zoukankan      html  css  js  c++  java
  • 汉字转化为拼音

    使用pinyin4j.jar即可。

    package com.wangzhu.pinyin;
    
    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;
    
    public class Pinyin {
        public static void main(String[] args) {
            Pinyin pinyin = new Pinyin();
            System.out.println(pinyin.getPinyin("好呀!呵呵!Welcome to ."));
            System.out.println(pinyin.getPinyinHeadChar("好呀!呵呵!Welcome to ."));
        }
    
        /**
         * 返回中文拼音
         * 
         * @param hanzi
         *            中文
         * @return 中文拼音
         */
        private String getPinyin(String hanzi) {
            StringBuffer pinyin = new StringBuffer();
            char[] hanziChar = hanzi.toCharArray();
            HanyuPinyinOutputFormat pinyinOutputFormat = new HanyuPinyinOutputFormat();
            pinyinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
            pinyinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
            pinyinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
            try {
                for (int i = 0, len = hanziChar.length; i < len; i++) {
                    if (Character.toString(hanziChar[i]).matches(
                            "[\\u4E00-\\u9FA5]+")) {
    
                        String[] tempArr = PinyinHelper.toHanyuPinyinStringArray(
                                hanziChar[i], pinyinOutputFormat);
    
                        pinyin.append(tempArr[0]);
                    } else {
                        pinyin.append(Character.toString(hanziChar[i]));
                    }
                }
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
            return pinyin.toString();
        }
    
        /**
         * 返回中文拼音的首字母
         * 
         * @param hanzi
         *            中文
         * @return 中文拼音首字母
         */
        protected String getPinyinHeadChar(String hanzi) {
            StringBuffer pinyinHead = new StringBuffer();
            for (int i = 0, len = hanzi.length(); i < len; i++) {
                char tempChar = hanzi.charAt(i);
                String[] tempArr = PinyinHelper.toHanyuPinyinStringArray(tempChar);
                if (null != tempArr) {
                    pinyinHead.append(tempArr[0].charAt(0));
                } else {
                    pinyinHead.append(tempChar);
                }
            }
            return pinyinHead.toString();
        }
    }

    结果:

    haoya!hehe!Welcome to .
    hy!hh!Welcome to .


    以下是反射机制的调用:

    package com.wangzhu.reflect;
    
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    
    public class TextReflect {
    
        /**
         * @param args
         * @throws ClassNotFoundException
         * @throws NoSuchMethodException
         * @throws SecurityException
         * @throws InstantiationException
         * @throws InvocationTargetException
         * @throws IllegalAccessException
         * @throws IllegalArgumentException
         */
        public static void main(String[] args) throws ClassNotFoundException,
                SecurityException, NoSuchMethodException, IllegalArgumentException,
                IllegalAccessException, InvocationTargetException,
                InstantiationException {
            Class<?> classForName = Class.forName("com.wangzhu.pinyin.Pinyin");
            Object obj = classForName.newInstance();
            Method[] methods = classForName.getDeclaredMethods();
            for (int i = 0, len = methods.length; i < len; i++) {
                System.out.println(methods[i].getName());
            }
            Method method = classForName.getDeclaredMethod("getPinyin",
                    String.class);
            method.setAccessible(true);// 只有设置为true,才能访问该私有方法
            Object temp = method.invoke(obj, "开始啦!恩雅 !之乎者也一眼在!Welcome the World!@");
            System.out.println(temp);
        }
    }


    结果:

    main
    getPinyin
    getPinyinHeadChar
    kaishila!enya !zhihuzheyeyiyanzai!Welcome the World!@

     

  • 相关阅读:
    Maven 建立的项目resource对应的实际位置
    MySQL常用查询语句汇总(不定时更新.......)
    Eclipse中使用Maven新建 Servlet 2.5的 SpringMVC项目
    Java异常(输出[D@139a55问题)
    JSP的原理、JSP的执行过程
    使用CMD命令行来对MySQL数据库执行迁移、备份、恢复
    Tomcat项目部署方式
    Java网络编程小结 URLConnection协议处理器
    java 多线程下载文件 以及URLConnection和HttpURLConnection的区别
    java中的==、equals()、hashCode()
  • 原文地址:https://www.cnblogs.com/xiaoxian1369/p/2849560.html
Copyright © 2011-2022 走看看