Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert Chinese into Pinyin in java

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, Xiaobian will bring you about how to convert Chinese into Pinyin in java. The article is rich in content and analyzes and narrates from a professional perspective. After reading this article, I hope you can gain something.

PinYin4jUtils

package com.gwl.bos.utils;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 PinYin4jUtils { /** * Convert a string to a pinyin array * * @param src * @return */ public static String[] stringToPinyin(String src) { return stringToPinyin(src, false, null); } /** * Convert a string to a Pinyin array * * @param src * @return */ public static String[] stringToPinyin(String src, String separator) { return stringToPinyin(src, true, separator); } /** * Convert a string to a pinyin array * * @param src * @param isPolyphone Did you find all the pinyin of polyphone * @param separator Separator between polysyllabic letters * @return */ public static String[] stringToPinyin(String src, boolean isPolyphone, String separator) { //determine whether the string is empty if ("".equals(src) || null == src) { return null; } char[] srcChar = src.toCharArray(); int srcCount = srcChar.length; String[] srcStr = new String[srcCount]; for (int i = 0; i

< srcCount; i++) { srcStr[i] = charToPinyin(srcChar[i], isPolyphone, separator); } return srcStr; } /** * 将单个字符转换成拼音 * * @param src * @return */ public static String charToPinyin(char src, boolean isPolyphone, String separator) { // 创建汉语拼音处理类 HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); // 输出设置,大小写,音标方式 defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); StringBuffer tempPinying = new StringBuffer(); // 如果是中文 if (src >

128) { try { // 转换得出结果 String[] strs = PinyinHelper.toHanyuPinyinStringArray(src, defaultFormat); // 是否查出多音字,默认是查出多音字的第一个字符 if (isPolyphone && null != separator) { for (int i = 0; i < strs.length; i++) { tempPinying.append(strs[i]); if (strs.length != (i + 1)) { // 多音字之间用特殊符号间隔起来 tempPinying.append(separator); } } } else { tempPinying.append(strs[0]); } } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } } else { tempPinying.append(src); } return tempPinying.toString(); } public static String hanziToPinyin(String hanzi) { return hanziToPinyin(hanzi, " "); } /** * 将汉字转换成拼音 * * @param hanzi * @param separator * @return */ public static String hanziToPinyin(String hanzi, String separator) { // 创建汉语拼音处理类 HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); // 输出设置,大小写,音标方式 defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); String pinyingStr = ""; try { pinyingStr = PinyinHelper.toHanYuPinyinString(hanzi, defaultFormat, separator, true); } catch (BadHanyuPinyinOutputFormatCombination e) { // TODO Auto-generated catch block e.printStackTrace(); } return pinyingStr; } /** * 将字符串数组转换成字符串 * * @param str * @param separator 各个字符串之间的分隔符 * @return */ public static String stringArrayToString(String[] str, String separator) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < str.length; i++) { sb.append(str[i]); if (str.length != (i + 1)) { sb.append(separator); } } return sb.toString(); } /** * 简单的将各个字符数组之间连接起来 * * @param str * @return */ public static String stringArrayToString(String[] str) { return stringArrayToString(str, ""); } /** * 将字符数组转换成字符串 * * @param * @param separator 各个字符串之间的分隔符 * @return */ public static String charArrayToString(char[] ch, String separator) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < ch.length; i++) { sb.append(ch[i]); if (ch.length != (i + 1)) { sb.append(separator); } } return sb.toString(); } /** * 将字符数组转换成字符串 * * @param * @return */ public static String charArrayToString(char[] ch) { return charArrayToString(ch, " "); } /** * 取汉字的首字母 * * @param src * @param isCapital 是否是大写 * @return */ public static char[] getHeadByChar(char src, boolean isCapital) { // 如果不是汉字直接返回 if (src

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report