Chinaunix首页 | 论坛 | 博客
  • 博客访问: 614277
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2021-06-29 10:17:13


pom.xml导入pinyin4j的依赖

 

点击(此处)折叠或打开

  1. <dependency>
  2. <groupId>com.belerweb</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.1</version>
  3. </dependency>

汉字转拼音工具类

点击(此处)折叠或打开

  1. package org.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.  * 说明:Freemarker 模版引擎类
  10.  * 作者:FH Admin
  11.  * from:fhadmin.org
  12.  */
  13. public class GetPinyin {

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

  46.     /**
  47.      * 得到中文首字母
  48.      * @param str
  49.      * @return
  50.      */
  51.     public static String getPinYinHeadChar(String str) {

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

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

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

  79.         String cnStr = "中国";
  80.         System.out.println(getPingYin(cnStr));
  81.         System.out.println(getPinYinHeadChar(cnStr));
  82.     }

  83. }



阅读(1295) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~