打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
中文转换为全拼和首字母
public class CnToPy {

//将汉字转换为全拼
public static String getPingYin(String src){

    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));
}

}
下面为结果:
zhonghuarenmingongheguo
zhrmghg
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
pinyin4j简介
java实现汉字转换为拼音
使用pinyin4j将中文转换为汉语拼音
oracle将汉字转为全拼或返回拼音首字母sql
Java split方法和正则表达式
java基础习题(三)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服