zoukankan      html  css  js  c++  java
  • 获取中文汉字字符串相应的拼音和首字母的大小写

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

    一、内容

      使用 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 zfc
    100      *
    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 chinese
    131      * @return
    132      * @author zfc
    133      *
    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 }

    三、运行结果

  • 相关阅读:
    函数式宏定义与普通函数
    linux之sort用法
    HDU 4390 Number Sequence 容斥原理
    HDU 4407 Sum 容斥原理
    HDU 4059 The Boss on Mars 容斥原理
    UVA12653 Buses
    UVA 12651 Triangles
    UVA 10892
    HDU 4292 Food
    HDU 4288 Coder
  • 原文地址:https://www.cnblogs.com/zfc-java/p/8065885.html
Copyright © 2011-2022 走看看