import com.github.stuxuhai.jpinyin.ChineseHelper;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper; /**
* @ClassName: ChineseConvertPinyinUtil
* @Description: 汉语繁体拼音转换工具类
*/
public final class ChineseConvertPinyinUtil {
private static final String SPACEMARK = "";
private ChineseConvertPinyinUtil() {} /**
* 检查汉字是否为多音字
* @param pinYinStr 需转换的汉字
* @param deleteBlank 转换后去掉非打印字符
* @param spaceMark 非打印字符
* @return true 多音字,false 不是多音字
*/
public static boolean checkPinYin(char pinYinStr) {
boolean check = false;
try {
check = PinyinHelper.hasMultiPinyin(pinYinStr);
} catch (Exception e) {
e.printStackTrace();
}
return check;
} /**
* 转换为每个汉字对应拼音首字母字符串
* @param pinYinStr 需转换的汉字
* @param deleteBlank 转换后去掉非打印字符
* @param spaceMark 非打印字符
* @return 拼音字符串
*/
public static String convertToGetShortPinYin(String pinYinStr, boolean deleteBlank) {
String tempStr = null;
try {
tempStr = PinyinHelper.getShortPinyin(pinYinStr);
} catch (Exception e) {
tempStr = pinYinStr;
e.printStackTrace();
}
if (deleteBlank) {
tempStr = tempStr.replaceAll("\\s*", SPACEMARK);
}
return tempStr;
} /**
* 转换为有声调的拼音字符串
* @param pinYinStr 汉字
* @param deleteBlank 转换后去掉非打印字符
* @param spaceMark 非打印字符
* @return 有声调的拼音字符串
*/
public static String convertToMarkPinYin(String pinYinStr, boolean deleteBlank, String spaceMark) {
String tempStr = pinYinStr;
try {
if (spaceMark == null) {
spaceMark = SPACEMARK;
}
tempStr = PinyinHelper.convertToPinyinString(pinYinStr, spaceMark, PinyinFormat.WITH_TONE_MARK); } catch (Exception e) {
tempStr = pinYinStr;
e.printStackTrace();
}
if (deleteBlank) {
tempStr = tempStr.replaceAll("\\s*", SPACEMARK);
}
return tempStr;
} /**
* 转换为数字声调字符串
* @param pinYinStr 需转换的汉字
* @param deleteBlank 转换后去掉非打印字符
* @param spaceMark 非打印字符
* @return 转换完成的拼音字符串
*/
public static String convertToNumberPinYin(String pinYinStr, boolean deleteBlank, String spaceMark) {
String tempStr = null;
try {
if (spaceMark == null) {
spaceMark = SPACEMARK;
}
tempStr = PinyinHelper.convertToPinyinString(pinYinStr, spaceMark, PinyinFormat.WITH_TONE_NUMBER);
} catch (Exception e) {
tempStr = pinYinStr;
e.printStackTrace();
}
if (deleteBlank) {
tempStr = tempStr.replaceAll("\\s*", SPACEMARK);
}
return tempStr;
} /**
* 繁体转换为简体
* @param pinYinSt
* @param deleteBlank 转换后去掉非打印字符
* @return
*/
public static String convertToSimplified(String pinYinSt, boolean deleteBlank) {
String tempStr = null;
try {
tempStr = ChineseHelper.convertToSimplifiedChinese(pinYinSt);
} catch (Exception e) {
tempStr = pinYinSt;
e.printStackTrace();
}
if (deleteBlank) {
tempStr = tempStr.replaceAll("\\s*", SPACEMARK);
}
return tempStr;
} /**
* 转换为不带音调的拼音字符串
* @param pinYinStr 需转换的汉字
* @param deleteBlank 转换后去掉非打印字符
* @param spaceMark 非打印字符
* @return 拼音字符串
*/
public static String convertToTonePinYin(String pinYinStr, boolean deleteBlank, String spaceMark) {
String tempStr = null;
try {
if (spaceMark == null) {
spaceMark = SPACEMARK;
}
tempStr = PinyinHelper.convertToPinyinString(pinYinStr, spaceMark, PinyinFormat.WITHOUT_TONE);
} catch (Exception e) {
tempStr = pinYinStr;
e.printStackTrace();
}
if (deleteBlank) {
tempStr = tempStr.replaceAll("\\s*", SPACEMARK);
}
return tempStr;
} /**
* 简体转换为繁体
* @param pinYinStr
* @param deleteBlank 转换后去掉非打印字符
* @return
*/
public static String convertToTraditional(String pinYinStr, boolean deleteBlank) {
String tempStr = null;
try {
tempStr = ChineseHelper.convertToTraditionalChinese(pinYinStr);
} catch (Exception e) {
tempStr = pinYinStr;
e.printStackTrace();
}
if (deleteBlank) {
tempStr = tempStr.replaceAll("\\s*", SPACEMARK);
}
return tempStr;
} public static void main(String[] args) {
String str = "重慶 most input \r\n a b c# d";
System.out.println(convertToSimplified(str, true));
System.out.println(checkPinYin('重'));
System.out.println(convertToMarkPinYin(str, true, null));
System.out.println(convertToGetShortPinYin(str, true));
System.out.println(convertToNumberPinYin(str, true, null));
System.out.println(convertToTonePinYin(str, true, null));
System.out.println(convertToTraditional(str, true));
}
}

依赖:

<dependency>

<groupId>com.github.stuxuhai</groupId>

<artifactId>jpinyin</artifactId>

<version>1.1.8</version>

</dependency>

Java-汉字繁体拼音转换的更多相关文章

  1. [转]Java汉字按照拼音排序

    最近项目上使用到汉字排序的问题,网上搜索了一下后普遍使用下面的方法比较. @Test public void test_sort_pinyin() { Collator cmp = Collator. ...

  2. java 汉字转拼音

    先决条件: pinyin4j.jar(Pinyin4j是一个流行的Java库,支持中文字符和拼音之间的转换.拼音输出格式可以定制.) 下载地址:http://pan.baidu.com/share/l ...

  3. java汉字转拼音以及得到首字母通用方法

    package oa.common.utils;   import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.piny ...

  4. java汉字转拼音的工具类

    import com.google.common.base.Strings;import net.sourceforge.pinyin4j.PinyinHelper;import net.source ...

  5. Java汉字转拼音

    import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCase ...

  6. Android Java汉字转拼音总结

    转载请表明出处:http://blog.csdn.net/lmj623565791/article/details/23187701 开发过程中有时候会遇到使用拼音模糊搜索等功能(典型的就是Andro ...

  7. JAVA汉字转拼音(取首字母大写)

    import net.sourceforge.pinyin4j.PinyinHelper;import net.sourceforge.pinyin4j.format.HanyuPinyinCaseT ...

  8. 用jpinyin实现汉字转拼音功能

    一.简介 项目地址:https://github.com/stuxuhai/jpinyin JPinyin是一个汉字转拼音的Java开源类库,在PinYin4j的功能基础上做了一些改进. [JPiny ...

  9. 汉字转拼音开源工具包Jpinyin介绍

    最近要实现一个根据词语得到词语对应拼音的功能,找到了Jpinyin这个开源工具包,使用下来发现它非常强大,完全满足我的需求,下面对它做一个简单的介绍,希望能够帮助到有需要的朋友. https://gi ...

随机推荐

  1. 优化RequireJS项目(合并与压缩)

    关于RequireJS已经有很多文章介绍过了.这个工具可以将你的JavaScript代码轻易的分割成苦 干个模块(module)并且保持你的代码模块化与易维护性.这样,你将获得一些具有互相依赖关系的J ...

  2. 如何在 Linux 上使用 x2go 设置远程桌面

    https://linux.cn/article-5708-1.html 由于一切都迁移到了云上,作为提高职员生产力的一种方式,虚拟远程桌面在工业中越来越流行.尤其对于那些需要在多个地方和设备之间不停 ...

  3. 快速排序算法-python实现

    #-*- coding: UTF-8 -*- import numpy as np def Partition(a, i, j): x = a[i] #将数组的第一个元素作为初始基准位置 p = i ...

  4. 【备忘录】yii2高级模板多个应用启用同一个域名多个栏目

    nginx部署方式,两种写法,本人认为第一种写法没有第二种写法优雅 第一种写法配置文件: server { listen ; server_name youban-dev.jqtest.mopon.c ...

  5. jmeter踩坑系列

    1.踩坑系列一: 抓包出来有host的字段,放到jmeter里面一起请求就报错了,去掉就请求正常了 1.踩坑系列二: 从花瓶复制过去 的values 前面有空格,肉眼看起来没有

  6. HBase之八--(2):HBase二级索引之Phoenix

    1. 介绍 Phoenix 是 Salesforce.com 开源的一个 Java 中间件,可以让开发者在Apache HBase 上执行 SQL 查询.Phoenix完全使用Java编写,代码位于 ...

  7. lnmp环境自动化部署

    lnmp.sh #!/bin/bash#This project to install lnmp#Author:菜逼cd命令玩家#Time:2016.10.13#objective:简化人工手动操作, ...

  8. 初识python(python的安装与运行)

    python--“优雅”.“明确”.“简单”的哲学定位 一.python的安装(Windows环境下) 1.在python官网下载安装文件 python的官方网址:https://www.python ...

  9. thymeleaf switch在表格中的使用,遇到的空行问题

    switch在表格中的使用时 如果把<td>写在<div th:switch="${data.isShow}"> 里面导致外面出现很多空的<div&g ...

  10. 【转】Android Eclipse调试技巧

    原文地址:https://www.cnblogs.com/tianchunming/p/5423942.html Android Eclipse调试技巧   在Android 应用程序开发中我们经常需 ...