免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1323 | 回复: 0
打印 上一主题 下一主题

中文获取全拼功能代码 [复制链接]

论坛徽章:
1
操作系统版块每日发帖之星
日期:2015-07-07 22:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-06 14:20 |只看该作者 |倒序浏览
[Java]代码
  1. package com.fh.util;
  2. import net.sourceforge.pinyin4j.PinyinHelper;
  3. import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
  4. import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
  5. import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
  6. import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
  7. import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

  8. /**
  9. * @explain:中文获取全拼功能代码
  10. *
  11. * @author:
  12. *
  13. * @version:
  14. */
  15. public class GetPinyin {

  16.     /**
  17.      * 得到 全拼
  18.      *
  19.      * @param src
  20.      * @return
  21.      */
  22.     public static String getPingYin(String src) {
  23.         char[] t1 = null;
  24.         t1 = src.toCharArray();
  25.         String[] t2 = new String[t1.length];
  26.         HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
  27.         t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
  28.         t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
  29.         t3.setVCharType(HanyuPinyinVCharType.WITH_V);
  30.         String t4 = "";
  31.         int t0 = t1.length;
  32.         try {
  33.             for (int i = 0; i < t0; i++) {
  34.                 // 判断是否为汉字字符
  35.                 if (java.lang.Character.toString(t1[i]).matches(
  36.                         "[\\u4E00-\\u9FA5]+")) {
  37.                     t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
  38.                     t4 += t2[0];
  39.                 } else {
  40.                     t4 += java.lang.Character.toString(t1[i]);
  41.                 }
  42.             }
  43.             return t4;
  44.         } catch (BadHanyuPinyinOutputFormatCombination e1) {
  45.             e1.printStackTrace();
  46.         }
  47.         return t4;
  48.     }

  49.     /**
  50.      * 得到中文首字母
  51.      *
  52.      * @param str
  53.      * @return
  54.      */
  55.     public static String getPinYinHeadChar(String str) {

  56.         String convert = "";
  57.         for (int j = 0; j < str.length(); j++) {
  58.             char word = str.charAt(j);
  59.             String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
  60.             if (pinyinArray != null) {
  61.                 convert += pinyinArray[0].charAt(0);
  62.             } else {
  63.                 convert += word;
  64.             }
  65.         }
  66.         return convert;
  67.     }

  68.     /**
  69.      * 将字符串转移为ASCII码
  70.      *
  71.      * @param cnStr
  72.      * @return
  73.      */
  74.     public static String getCnASCII(String cnStr) {
  75.         StringBuffer strBuf = new StringBuffer();
  76.         byte[] bGBK = cnStr.getBytes();
  77.         for (int i = 0; i < bGBK.length; i++) {
  78.             // System.out.println(Integer.toHexString(bGBK[i]&0xff));
  79.             strBuf.append(Integer.toHexString(bGBK[i] & 0xff));
  80.         }
  81.         return strBuf.toString();
  82.     }

  83.     public static void main(String[] args) {

  84.         String cnStr = "中国";
  85.         System.out.println(getPingYin(cnStr));
  86.         System.out.println(getPinYinHeadChar(cnStr));
  87.     }

  88. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP