博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
获取中文汉字字符串相应的拼音和首字母的大小写
阅读量:4575 次
发布时间:2019-06-08

本文共 5734 字,大约阅读时间需要 19 分钟。

获取中文汉字字符串相应的拼音和首字母的大小写

一、内容

  使用 pinyin4j 技术,获取一个字符串相应的拼音和首字符的大小写。

  需要 pinyin4j 的组建包,官网下载地址:http://pinyin4j.sourceforge.net/

  官方文档:http://pinyin4j.sourceforge.net/pinyin4j-doc/

二、源代码

1 package cn.com.zfc.day014;  2   3 import net.sourceforge.pinyin4j.PinyinHelper;  4 import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;  5 import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;  6 import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;  7 import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;  8   9 /** 10  *  11  * @describe 中文转化成拼音工具类 12  * @author zfc 13  * @date 2017年12月19日 上午11:32:59 14  */ 15 public class ChineseToPinYinUtil { 16  17     public static void main(String[] args) { 18         // 中文字符串首字母大写 19         System.out.println("张富昌(中文字符串首字母大写):" + toFirstCharUpCase("张富昌")); 20         // 中文字符串首字母小写 21         System.out.println("张富昌(中文字符串首字母小写):" + toFirstCharLowCase("张富昌")); 22         // 中文字符串拼音大写 23         System.out.println("张富昌(中文字符串拼音大写):" + toPinyinUpCase("张富昌")); 24         // 中文字符串拼音小写 25         System.out.println("张富昌(中文字符串拼音小写):" + toPinyinLowCase("张富昌")); 26     } 27  28     /** 29      * 获取中文字符串相应的拼音首字母小写 30      *  31      * @param chinese 32      * @return 33      * @author zfc 34      * 35      */ 36     public static String toFirstCharLowCase(String chinese) { 37         if (null == chinese) { 38             return null; 39         } 40         String pinyinStr = ""; 41         // 将字符串转为字符数组 42         char[] newChar = chinese.toCharArray(); 43         HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); 44         // 设置小写 45         defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); 46         defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); 47         for (int i = 0; i < newChar.length; i++) { 48             if (newChar[i] > 128) { 49                 try { 50                     pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0); 51                 } catch (BadHanyuPinyinOutputFormatCombination e) { 52                     e.printStackTrace(); 53                 } 54             } else { 55                 pinyinStr += newChar[i]; 56             } 57         } 58         return pinyinStr; 59     } 60  61     /** 62      * 获取中文字符串相应的拼音首字母大写 63      *  64      * @param chinese 65      * @return 66      * @author zfc 67      * 68      */ 69     public static String toFirstCharUpCase(String chinese) { 70         if (null == chinese) { 71             return null; 72         } 73         String pinyinStr = ""; 74         // 将字符串转为字符数组 75         char[] newChar = chinese.toCharArray(); 76         HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); 77         // 设置大写 78         defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE); 79         defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); 80         for (int i = 0; i < newChar.length; i++) { 81             if (newChar[i] > 128) { 82                 try { 83                     pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0); 84                 } catch (BadHanyuPinyinOutputFormatCombination e) { 85                     e.printStackTrace(); 86                 } 87             } else { 88                 pinyinStr += newChar[i]; 89             } 90         } 91         return pinyinStr; 92     } 93  94     /** 95      * 将中文字符串转换成拼音小写 96      *  97      * @param chinese 98      * @return 99      * @author zfc100      *101      */102     public static String toPinyinLowCase(String chinese) {103         if (null == chinese) {104             return null;105         }106         String pinyinStr = "";107         // 将字符串转为字符数组108         char[] newChar = chinese.toCharArray();109         HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();110         // 设置小写111         defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);112         defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);113         for (int i = 0; i < newChar.length; i++) {114             if (newChar[i] > 128) {115                 try {116                     pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];117                 } catch (BadHanyuPinyinOutputFormatCombination e) {118                     e.printStackTrace();119                 }120             } else {121                 pinyinStr += newChar[i];122             }123         }124         return pinyinStr;125     }126 127     /**128      * 将中文字符串转换成拼音大写129      * 130      * @param chinese131      * @return132      * @author zfc133      *134      */135     public static String toPinyinUpCase(String chinese) {136         if (null == chinese) {137             return null;138         }139         String pinyinStr = "";140         // 将字符串转为字符数组141         char[] newChar = chinese.toCharArray();142         HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();143         // 设置大写144         defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);145         defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);146         for (int i = 0; i < newChar.length; i++) {147             if (newChar[i] > 128) {148                 try {149                     pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];150                 } catch (BadHanyuPinyinOutputFormatCombination e) {151                     e.printStackTrace();152                 }153             } else {154                 pinyinStr += newChar[i];155             }156         }157         return pinyinStr;158     }159 }

三、运行结果

转载于:https://www.cnblogs.com/zfc-java/p/8065885.html

你可能感兴趣的文章
好程序员技术分享html5和JavaScript的区别
查看>>
好程序员web前端分享CSS3文本属性
查看>>
ThinkPHP3.0启动过程
查看>>
Java入门 之 类和对象(一) - 类
查看>>
16级第二周寒假作业E题
查看>>
Java实现Windows锁屏
查看>>
在线会话管理
查看>>
文本处理之可视化wordcloud
查看>>
SampleManager(赛默飞)
查看>>
堆排序
查看>>
我的面试问题记录
查看>>
函数PARSENAME使用和截取字符串
查看>>
关乎性能的判断,请作出果断选择
查看>>
判断是否包含指定的字符
查看>>
[Html5] HTML5 开发手机应用
查看>>
[工具] 各种主流 SQLServer 迁移到 MySQL 工具对比
查看>>
(二)Maven 基本概念——依赖、生命周期、仓库管理、聚合&继承
查看>>
py4CV例子3Mnist识别和ANN
查看>>
【4Opencv】如何识别出轮廓准确的长和宽
查看>>
现货黄金交易计划摸索
查看>>