Java: Pinying 拼音
/** * encoding: utf-8 * 版权所有 2023 涂聚文有限公司 * 许可信息查看: * 描述:https://github.com/houbb/pinyin * https://mvnrepository.com/artifact/com.github.stuxuhai/jpinyin/1.1.8 * https://github.com/ranLee1/jpinyin * https://github.com/shenkevin/jpinyin * https://github.com/hellokaton/TinyPinyin 速度快 * # Author : geovindu,Geovin Du 涂聚文. * # IDE : IntelliJ IDEA 2023.1 Java 17 * # Datetime : 2023 - 2023/12/16 - 6:18 * # User : geovindu * # Product : IntelliJ IDEA * # Project : javademo * # File : PinyinHelper.java 类 * # explain : 学习 **/ package Common; import java.util.Random; import java.lang.String; import java.io.UnsupportedEncodingException; import java.util.Random; //import opensource.jpinyin.ChineseHelper; //import opensource.jpinyin.PinyinFormat; //import opensource.jpinyin.PinyinHelper; import com.github.stuxuhai.jpinyin.ChineseHelper; import com.github.stuxuhai.jpinyin.PinyinFormat; import com.github.stuxuhai.jpinyin.PinyinHelper; import com.github.stuxuhai.jpinyin.*; /** * 汉字转拼音类 */ public class DuPinyinHelper { /** *有声调的拼音 * @param ChineseWord 中国的首都是北京 * @return */ public static String ShenTiao(String ChineseWord) { String pin=""; try { //带音标 zhōng,guó,de,shǒu,dū,shì,běi,jīng pin=com.github.stuxuhai.jpinyin.PinyinHelper.convertToPinyinString(ChineseWord,",",PinyinFormat.WITH_TONE_MARK); return pin; } catch (Exception ex) { System.out.println(ex.getMessage()); return null; } } /** * 用数字标的拼音 * @param ChineseWord * @return */ public static String ShengtiaoNum(String ChineseWord) { String pin=""; try { //用数字代替音标 zhong1,guo2,de5,shou3,du1,shi4,bei3,jing1 System.out.println(com.github.stuxuhai.jpinyin.PinyinHelper.convertToPinyinString(ChineseWord,",",PinyinFormat.WITH_TONE_NUMBER)); pin=com.github.stuxuhai.jpinyin.PinyinHelper.convertToPinyinString(ChineseWord,",",PinyinFormat.WITH_TONE_NUMBER); //不带音标 zhong,guo,de,shou,du,shi,bei,jing // System.out.println(com.github.stuxuhai.jpinyin.PinyinHelper.convertToPinyinString(ChineseWord, ",", PinyinFormat.WITHOUT_TONE)); // pin=com.github.stuxuhai.jpinyin.PinyinHelper.convertToPinyinString(ChineseWord, ",", PinyinFormat.WITHOUT_TONE); return pin; } catch (Exception ex) { System.out.println(ex.getMessage()); return null; } } /** * 无声调的拼音 * @param ChineseWord * @return */ public static String NoShengtiaoNum(String ChineseWord) { String pin=""; try { //不带音标 zhong,guo,de,shou,du,shi,bei,jing // System.out.println(com.github.stuxuhai.jpinyin.PinyinHelper.convertToPinyinString(ChineseWord, ",", PinyinFormat.WITHOUT_TONE)); pin=com.github.stuxuhai.jpinyin.PinyinHelper.convertToPinyinString(ChineseWord, ",", PinyinFormat.WITHOUT_TONE); return pin; } catch (Exception ex) { System.out.println(ex.getMessage()); return null; } } /** * 小写首字母 * @param ChineseWord * @return */ public static String ShengMuLower(String ChineseWord) { String pin=""; try { //System.out.println(com.github.stuxuhai.jpinyin.PinyinHelper.getShortPinyin(ChineseWord));//输出拼音首字母 小写 zgdsdsbj pin=com.github.stuxuhai.jpinyin.PinyinHelper.getShortPinyin(ChineseWord).toLowerCase(); return pin; } catch (Exception ex) { System.out.println(ex.getMessage()); return null; } } /** * 大写首字母 * @param ChineseWord * @return */ public static String ShengMuUpper(String ChineseWord) { String pin=""; try { //System.out.println(com.github.stuxuhai.jpinyin.PinyinHelper.getShortPinyin(ChineseWord));//输出拼音首字母 pin=com.github.stuxuhai.jpinyin.PinyinHelper.getShortPinyin(ChineseWord).toUpperCase(); return pin; } catch (Exception ex) { System.out.println(ex.getMessage()); return null; } } }
调用:
DuPinyinHelper.ShenTiao("我是涂聚文,江西人");
输出:
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)