首页 > 编程语言 > 详细

java提取每个汉字的首字母

时间:2014-03-23 10:16:03      阅读:539      评论:0      收藏:0      [点我收藏+]

  在项目中需要提取每个汉字的首字母,下面是工具类的源码:

import net.sourceforge.pinyin4j.PinyinHelper;

public class PinyinAPI {

    /**
     * 提取每个汉字的首字母(大写)
     * 
     * @param str
     * @return
     */
    public static String getPinYinHeadChar(String str) {
        if (isNull(str)) {
            return "";
        }
        String convert = "";
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            // 提取汉字的首字母
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert += pinyinArray[0].charAt(0);
            }
            else {
                convert += word;
            }
        }

        convert = string2AllTrim(convert);
        return convert.toUpperCase();
    }

    /*
     * 判断字符串是否为空
     */

    public static boolean isNull(Object strData) {
        if (strData == null || String.valueOf(strData).trim().equals("")) {
            return true;
        }
        return false;
    }

    /**
     * 去掉字符串包含的所有空格
     * 
     * @param value
     * @return
     */
    public static String string2AllTrim(String value) {
        if (isNull(value)) {
            return "";
        }
        return value.trim().replace(" ", "");
    }

    public static void main(String[] args) {
        String ss = PinyinAPI.getPinYinHeadChar("中国");
        System.out.print(ss);//ZG
    }
}

需要的jar包下载地址(免积分):http://download.csdn.net/detail/zl544434558/7076087

java提取每个汉字的首字母,布布扣,bubuko.com

java提取每个汉字的首字母

原文:http://blog.csdn.net/zl544434558/article/details/21703859

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!