pinyin4j 的应用

本文提供了一个简单的Java代码示例,利用Pinyin4j库实现了将中文字符串转换为拼音首字母的功能。包括中文字符串到拼音首字母的转换和中文字符串到完整拼音的转换,同时提供了参数控制是否忽略非中文字符和生成结果的大小写形式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


<!-- pinyin4j -->
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
</dependency>



import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class PinyinUtils {

private static final String CHINESE_REGEX = "[\u4e00-\u9fa5]";
private static final String EMPTY_STRING = "";
private static final String SPACE_STRING = " ";

/** 私有构造方法 */
private PinyinUtils() {
super();
}

// -----------------------------------------------------------------------------------------------------------------------

/**
* 生成中文拼音首字母
*
* @param chinese 中文字符串
* @return 中文拼音首字母
*/
public static String cn2FirstSpell(String chinese) {
return cn2FirstSpell(chinese, true);
}

/**
* 生成中文拼音首字母
*
* @param chinese 中文字符串
* @param ignoreNonChineseChar 是否忽略非中文字符
* @return 中文拼音首字母
*/
public static String cn2FirstSpell(String chinese, boolean ignoreNonChineseChar) {
return cn2FirstSpell(chinese, ignoreNonChineseChar, true);
}

/**
* 生成中文拼音首字母
*
* @param chinese 中文字符串
* @param ignoreNonChineseChar 是否忽略非中文字符
* @param lowerCase 生成结果是否为小写字母
* @return 中文拼音首字母
*/
public static String cn2FirstSpell(String chinese, boolean ignoreNonChineseChar, boolean lowerCase) {
return cn2FirstSpell(chinese, ignoreNonChineseChar, lowerCase, EMPTY_STRING);
}

/**
* 生成中文拼音首字母
*
* @param chinese 中文字符串
* @param ignoreNonChineseChar 是否忽略非中文字符
* @param lowerCase 生成结果是否为小写字母
* @param separator 分隔符
* @return 中文拼音首字母
*/
public static String cn2FirstSpell(String chinese, boolean ignoreNonChineseChar, boolean lowerCase, String separator) {

if (chinese == null || chinese.isEmpty()) {
return EMPTY_STRING;
}

HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setToneType(HanyuPinyinToneType.WITH_TONE_NUMBER);

if (lowerCase) {
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
} else {
format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
}

StringBuffer result = new StringBuffer();

char[] arr = chinese.toCharArray();
for (int i = 0; i < arr.length; i++) {
char c = arr[i];
if (! new String(new char[]{c}).matches(CHINESE_REGEX)) {
if (! ignoreNonChineseChar) {
result.append(c);
if (i != arr.length - 1) {
result.append(separator);
}
}
continue;
}
try {
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
if (pinyinArray != null && pinyinArray.length != 0) {
result.append(pinyinArray[0].charAt(0));
if (i != arr.length - 1) {
result.append(separator);
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
}
return result.toString().trim();
}

// -----------------------------------------------------------------------------------------------------------------------

/**
* 中文串换为拼音
*
* @param chinese 中文字符串
* @return 拼音
*/
public static String cn2Spell(String chinese) {
return cn2Spell(chinese, true);
}

/**
* 中文串换为拼音
*
* @param chinese 中文字符串
* @param ignoreNonChineseChar 是否忽略非中文字符
* @return 拼音
*/
public static String cn2Spell(String chinese, boolean ignoreNonChineseChar) {
return cn2Spell(chinese, ignoreNonChineseChar, true);
}

/**
* 中文串换为拼音
*
* @param chinese 中文字符串
* @param ignoreNonChineseChar 是否忽略非中文字符
* @param lowerCase 生成结果是否为小写字母
* @return 拼音
*/
public static String cn2Spell(String chinese, boolean ignoreNonChineseChar, boolean lowerCase) {
return cn2Spell(chinese, ignoreNonChineseChar, lowerCase, SPACE_STRING);
}

/**
* 中文串换为拼音
*
* @param chinese 中文字符串
* @param ignoreNonChineseChar 是否忽略非中文字符
* @param lowerCase 生成结果是否为小写字母
* @param separator 分隔符
* @return 拼音
*/
public static String cn2Spell(String chinese, boolean ignoreNonChineseChar, boolean lowerCase, String separator) {
if (chinese == null || chinese.isEmpty()) {
return EMPTY_STRING;
}

HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setToneType(HanyuPinyinToneType.WITH_TONE_NUMBER);

if (lowerCase) {
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
} else {
format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
}

StringBuffer result = new StringBuffer();

char[] arr = chinese.toCharArray();
for (int i = 0; i < arr.length; i++) {
char c = arr[i];
if (! new String(new char[]{c}).matches(CHINESE_REGEX)) {
if (! ignoreNonChineseChar) {
result.append(c);
if (i != arr.length - 1) {
result.append(separator);
}
}
continue;
}
try {
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
if (pinyinArray != null && pinyinArray.length != 0) {
result.append(pinyinArray[0].substring(0, pinyinArray[0].length() - 1));
if (i != arr.length - 1) {
result.append(separator);
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
}

return result.toString().trim();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值