Java-汉字繁体拼音转换
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-汉字繁体拼音转换的更多相关文章
- [转]Java汉字按照拼音排序
最近项目上使用到汉字排序的问题,网上搜索了一下后普遍使用下面的方法比较. @Test public void test_sort_pinyin() { Collator cmp = Collator. ...
- java 汉字转拼音
先决条件: pinyin4j.jar(Pinyin4j是一个流行的Java库,支持中文字符和拼音之间的转换.拼音输出格式可以定制.) 下载地址:http://pan.baidu.com/share/l ...
- java汉字转拼音以及得到首字母通用方法
package oa.common.utils; import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.piny ...
- java汉字转拼音的工具类
import com.google.common.base.Strings;import net.sourceforge.pinyin4j.PinyinHelper;import net.source ...
- Java汉字转拼音
import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCase ...
- Android Java汉字转拼音总结
转载请表明出处:http://blog.csdn.net/lmj623565791/article/details/23187701 开发过程中有时候会遇到使用拼音模糊搜索等功能(典型的就是Andro ...
- JAVA汉字转拼音(取首字母大写)
import net.sourceforge.pinyin4j.PinyinHelper;import net.sourceforge.pinyin4j.format.HanyuPinyinCaseT ...
- 用jpinyin实现汉字转拼音功能
一.简介 项目地址:https://github.com/stuxuhai/jpinyin JPinyin是一个汉字转拼音的Java开源类库,在PinYin4j的功能基础上做了一些改进. [JPiny ...
- 汉字转拼音开源工具包Jpinyin介绍
最近要实现一个根据词语得到词语对应拼音的功能,找到了Jpinyin这个开源工具包,使用下来发现它非常强大,完全满足我的需求,下面对它做一个简单的介绍,希望能够帮助到有需要的朋友. https://gi ...
随机推荐
- Spring Cloud微服务框架介绍
Spring Cloud为开发人员提供了一整套的快速构建分布式应用的工具,入服务注册.服务发现.熔断.负载均衡.路由等,提供了开箱即用的各种依赖以及良好的可扩展机制. 目前在Spring Cloud的 ...
- How to install torcs package in Debian
Installation instructions Installing torcs package in Debian Wheezy is as easy as running: 没想到在debia ...
- JavaSE 手写 Web 服务器(二)
原文地址:JavaSE 手写 Web 服务器(二) 博客地址:http://www.extlight.com 一.背景 在上一篇文章 <JavaSE 手写 Web 服务器(一)> 中介绍了 ...
- Linux make menuconfig报错 Your display is too small to run Menuconfig!
在没有全屏的状态下执行 make menuconfig,如果报下面的错误,表示终端的窗口太小,需要放大窗口或者全屏操作. ## using defaults found in /dev/null#Yo ...
- StreamTool
public class StreamTool { //从流中读取数据 public static byte[] read(InputStream inStream) throws Exception ...
- java代码---I/O文件内容复制
总结:主要是弄清输入流,输出流 输入流和输出流都是针对程序而言,把文件内容读取到程序是输入流 从程序把内容写入文件是输出流 package com.a.b; import java.io.*; imp ...
- JDK bin指令
jmap: 一.调用命令示例: jmap -F-dump:format=b,file=rmsheap.bin 6086 就会在当前目录下生成rmsheap.bin的文件,6086是Pid.-F是在某些 ...
- 【UVa】11212 Editing a Book(IDA*)
题目 题目 分析 get一下IDA*的技巧,感觉总体来说不难,主要是剪枝比较难想. 这是lrj的代码,比较通俗易懂,关键就是选定一个区间再取出来,插入到一个位置,接下来转移到这个状态. ...
- CentOS7 tar打包工具 打包,解包,打包压缩,打包解压缩
tar命令 選項與參數: -c :建立打包檔案,可搭配 -v 來察看過程中被打包的檔名(filename) -t :察看打包檔案的內容含有哪些檔名,重點在察看『檔名』就是了: -x :解打包或解壓縮的 ...
- 使用poi读写Excel------demo
package com.js.ai.modules.pointwall.interfac; import java.io.FileInputStream; import java.io.FileOut ...