JAVA汉字转拼音

 

 首先先要导入pinyin4j-2.5.0.jar 这个jar包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
    * 得到 全拼
    *
    * @param src
    * @return
    */
   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]);
               }
           }
           return t4;
       } catch (BadHanyuPinyinOutputFormatCombination e1) {
           e1.printStackTrace();
       }
       return t4;
   }
   /**
    * 得到中文首字母
    *
    * @param str
    * @return
    */
   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码
    *
    * @param cnStr
    * @return
    */
   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();
   }
 

  

posted @   yvioo  阅读(436)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示