汉字转首字母工具类


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 ChineseToFirstLetterUtil {
	/**
	 * 获取每个汉字首字母(大写)
	 * @param c
	 * @return
	 */
	public static String ToFirstBig(String c) {
        String string = "";
		char b;
		int a = c.length();
		for (int k = 0; k < a; k++) {
			b = c.charAt(k);
			String d = String.valueOf(b);
			String str = toAllSpell(d);
			String s = str.toUpperCase();
			String g = s;
			char h;
			int j = g.length();
			for (int y = 0; y <= 0; y++) {
				h = g.charAt(0);
				string += h;
			}
		}
		return string;
	}

	/**
	 * 获取中文的全拼(小写)
	 * @param chines
	 * @return
	 */
	public static String toAllSpell(String chines) {
		String pinyinName = "";
		char[] nameChar = chines.toCharArray();
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
		defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		for (int i = 0; i < nameChar.length; i++) {
			String s = String.valueOf(nameChar[i]);
			if (s.matches("[\\u4e00-\\u9fa5]")) {
				try {
					String[] mPinyinArray = PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat);
					pinyinName += mPinyinArray[0];
				} catch (BadHanyuPinyinOutputFormatCombination e) {
					e.printStackTrace();
				}
			} else {
				pinyinName += nameChar[i];
			}
		}
		return pinyinName;
	}

}
        <!--中文转拼音-->
        <dependency>
            <groupId>com.belerweb</groupId>
            <artifactId>pinyin4j</artifactId>
            <version>2.5.1</version>
        </dependency>
        <!--  拼音工具类  -->
        <dependency>
            <groupId>com.belerweb</groupId>
            <artifactId>pinyin4j</artifactId>
            <version>2.5.0</version>
        </dependency>

上一篇:seek for|contrary to|lag behind|take up|take advantage of|be confident of|allow for |


下一篇:VScode编译并运行C/C++文件