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 CN2EN {

// 将汉字转换为全拼
    public static String getPingYin(String src) {
        src=src.replaceAll("(", "(").replaceAll(")", ")");
        char[] t1 = null;
        t1 = src.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++) {
                // 判断是否为汉字字符
                if (java.lang.Character.toString(t1[i]).matches(
                        "[\\u4E00-\\u9FA5]+")) {
                    t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
                    t4 += t2[0];
                } else
                    t4 += java.lang.Character.toString(t1[i]);
            }
            // System.out.println(t4);
            return t4;
        } catch (BadHanyuPinyinOutputFormatCombination e1) {
            e1.printStackTrace();
        }
        return t4;
    }

// 返回中文的首字母
    public static String getPinYinHeadChar(String str) {

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;
            }
        }
        return convert;
    }

// 将字符串转移为ASCII码
    public static String getCnASCII(String cnStr) {
        StringBuffer strBuf = new StringBuffer();
        byte[] bGBK = cnStr.getBytes();
        for (int i = 0; i < bGBK.length; i++) {
            // System.out.println(Integer.toHexString(bGBK[i]&0xff));
            strBuf.append(Integer.toHexString(bGBK[i] & 0xff));
        }
        return strBuf.toString();
    }

public static void main(String[] args) {

String cnStr = "中华人民共和国";
        System.out.println(getPingYin(cnStr));
        System.out.println(getPinYinHeadChar(cnStr));
        System.out.println(getCnASCII(cnStr));
    }

}

jar下载地址 http://pan.baidu.com/s/1slaL3kT 提取码3gak

JAVA将汉字转换为全拼以及返回中文的首字母,将字符串转移为ASCII码的更多相关文章

  1. java根据汉字获取全拼和首字母

    import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCase ...

  2. 通过pinyin4j将汉字转换为全拼 和 拼音首字母

    /** * 汉字转换为拼音 包含多音字,包含生母zh,ch,sh的 */ public void toPinYinAll(){ String initials = "zh,ch,sh&quo ...

  3. vue 获取汉字的全拼、简拼、首拼

    1.封装公共方法,获取汉字的全拼.简拼.首拼 export const Pinyin = { _JMcode:{ "-":"", "—":& ...

  4. ASP.NET Core中返回 json 数据首字母大小写问题

    ASP.NET Core中返回 json 数据首字母大小写问题 在asp.net core中使用ajax请求动态绑定数据时遇到该问题 后台返回数据字段首字母为定义的大写,返回的数据没有问题 但是在前台 ...

  5. Java 获取汉字串首字母并大写和获取汉字的全拼,英文字符不变

    在开发中我们难免会遇到需要提出汉字中的拼音的首字母.提出汉字的拼音,接着便介绍一个工具类 pinyin4j.jar,首先需要下载 jar 包. Pinyin4j是一个功能强悍的汉语拼音工具包,是sou ...

  6. JAVA实现汉字转换为拼音 pinyin4j/JPinyin

    在项目中经常会遇到需求用户输入汉字后转换为拼音的场景,比如说通讯录,就会要求按名字首字符发音排序,如果自己写实现这方面的功能是个很好大的工程,还好网上有公开的第三方jar支持转换,结合网上很多前辈的代 ...

  7. MySQL通过函数获取字符串汉字拼音首字母大写字符串

    DELIMITER $$ DROP FUNCTION IF EXISTS `Fun_GetPY`$$ CREATE FUNCTION `HIS`.`Fun_GetPY` (in_string VARC ...

  8. Java序列化,解决字段为null与序列化后首字母变小写问题

    fastjson.jar package com.apt.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.se ...

  9. 【Java】使用pinyin4j获取汉字的全拼或首字母

    汉字转拼音的工具类,常用于做汉字拼音的模糊查询. https://www.cnblogs.com/htyj/p/7891918.html

随机推荐

  1. Jmeter接口测试+压力测试+环境配置+证书导出

    jmeter是apache公司基于java开发的一款开源压力测试工具,体积小,功能全,使用方便,是一个比较轻量级的测试工具,使用起来非常简单.因为jmeter是java开发的,所以运行的时候必须先要安 ...

  2. bzoj4361 isn(树状数组优化dp+容斥)

    4361: isn Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 938  Solved: 485[Submit][Status][Discuss] ...

  3. 3. Dubbo原理解析-Dubbo内核实现之动态编译 (转)

    转载自  斩秋的专栏  http://blog.csdn.net/quhongwei_zhanqiu/article/details/41577159 我们运行的Java代码,一般都是编译之后的字节码 ...

  4. 8th,常用模块、正则表达式

    re模块 什么是正则? 正则就是用一些具有特殊含义的符号组合到一起(正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则.内嵌在Python中,通过re模块实现.正则表达式模 ...

  5. 移动Web UI库(H5框架)

    1.Framework7 Framework7 - is a free and open source mobile HTML framework to develop hybrid mobile a ...

  6. C#---装箱、拆箱的一个案例

    using System; namespace ConsoleApplication1 { interface IInterface { void Add(int num); } struct Tes ...

  7. hibernate 的API使用

    1.Query对象:不需要写sql语句,但需要hql语句,和sql很类似 (1)sql和hql区别:sql操作表和表字段,hql操作实体和实体属性 (2)使用: 2.Criteria对象:不需要写语句 ...

  8. 查看CPU温度

    因为不喜欢鲁大师,所以检索看有没有别的软件. 1)先是找到了aida64, 结果好像是要付费的,就没装. 2)因为自己也就想看cpu温度,于是寻思自己编程解决. 找到了下面的文章. 不但介绍了怎么编程 ...

  9. C# 获取CPU序列号、网卡MAC地址、硬盘序列号封装类,用于软件绑定电脑

    using System.Management; namespace GLaLa { /// <summary> /// hardware_mac 的摘要说明. /// </summ ...

  10. python3中 getpass模块使用

    getpass在IDLE中报错 CMD中可以使用 import getpass usr = getpass.getuser print(usr) 返回值为当前windows登陆用户名