import com.acmenxd.logger.Logger;
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.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
/**
* @author AcmenXD
* @version v1.0
* @github https://github.com/AcmenXD
* @date 2016/12/15 16:10
* @detail 拼音工具类
*/
public class PinYinUtils {
/**
* 将inStr中的中文转化为拼音,其他字符保持不变
* * 中文转换的拼音为 小写字母
*/
public static String parsePinyin(String inStr) {
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
format.setVCharType(HanyuPinyinVCharType.WITH_V);
char[] input = inStr.trim().toCharArray();
StringBuilder output = new StringBuilder();
try {
for (int i = 0; i < input.length; i++) {
if (Character.toString(input[i]).matches(
"[\\u4E00-\\u9FA5]+")) {
String[] temp = PinyinHelper.toHanyuPinyinStringArray(
input[i], format);
if (temp != null && temp.length > 0) {
output.append(temp[0]);
} else {
Logger.e("parsePinyin ? " + inStr);
}
} else
output.append(Character.toString(input[i]));
}
} catch (BadHanyuPinyinOutputFormatCombination pE) {
Logger.e(pE);
}
return output.toString();
}
/**
* 将inStr的中文字符,转换为中文拼音的首字母,其他字符保持不变
* * 中文转换的首字母为 大写字母
*/
public static String parsePinyinToFirst(String inStr) {
String pinyinName = "";
char[] nameChar = inStr.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < nameChar.length; i++) {
if (nameChar[i] > 128) {
try {
pinyinName += PinyinHelper.toHanyuPinyinStringArray(
nameChar[i], defaultFormat)[0].charAt(0);
} catch (BadHanyuPinyinOutputFormatCombination pE) {
Logger.e(pE);
}
} else {
pinyinName += nameChar[i];
}
}
return pinyinName;
}
/**
* 判断字符是否是中文
*/
public static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
return true;
}
return false;
}
}
pinyin4j 拼音中文工具类
最新推荐文章于 2024-04-29 17:29:35 发布